DEV Community

loading...
Cover image for JavaScript Made Easy: Part 4

JavaScript Made Easy: Part 4

dtetreau profile image David Tetreau Updated on ・2 min read

For this post, we will go over various topics such as comments, assignment operators, and arithmetic operators. As always, open up a repl and code along with this post. You learn more by doing it yourself, and you will build muscle memory. Log all of these operations to the console in your repl and use comments to reinforce what you are doing.

Comments

Comments are fairly easy to understand. There are two types of comments in JavaScript. The first is a single-line comment. The second is a multi-line comment. Here are some examples:

// This is a single-line comment.

/* 
This is a multi-line comment.
Everything inside of this comment
will not be run. You can also 
use comments for not only notes, 
but you can comment out a block of
code that you want to leave out but
not delete
*/
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

There are several different types of operators in JavaScript:
Assignment operators assign a value to a variable. You learned about this in previous posts.

const currentLesson = 4; //assignment operator
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operators

  • Addition

The addition operator is used for adding numbers, adding booleans, adding variables, and for combining strings together.

// expected result: 4
2 + 2

// expected result: 3
2 + true

/* 
expected result: "I am a Developer"
Notice that there had to be a space 
added at the end of the first string
*/
'I am a ' + 'Developer'

//expected result: '2001 is my graduation year'
2001 + ' is my graduation year'
Enter fullscreen mode Exit fullscreen mode
  • Multiplication

The multiplication operator multiplies numbers or numbers stored in variables. Here is an example:

//multiplying numbers
5 * 3 //equals 15 

//multiplying variables
const number1 = 5;
const number2 = 3; 
const number3 = number1 * number2 // equals 15
Enter fullscreen mode Exit fullscreen mode
  • Other operators that can be used to do arithmetic the same way are:
5 - 5 //subtraction|

/*
exponentiation
expected output is 81
same as 3 to the 4th power
*/
3 ** 4

1 / 2 //division

/*modulus
returns the remainder 
after division
*/
12 % 5 

++ // increment (increases a number by 1)

-- // decrement (decreases a number by 1)
Enter fullscreen mode Exit fullscreen mode

I hope you have enjoyed this post! Please check out the entire "JavaScript Made Easy" series by David Tetreau. There will be a new post daily.

Discussion (4)

Collapse
jonrandy profile image
Jon Randy

Your explanation of ++ and -- is overly simplistic. As you rightly say, they increment and decrement a variable by 1 - but the position of operator changes what is returned:

// Contrast this
a = 1
b = a++
console.log(a, b) // 2 1

// with this
a = 1
b = ++a
console.log(a, b) // 2 2
Enter fullscreen mode Exit fullscreen mode

The postfix version will return a then increment it, whereas the prefix version will increment a then return its new value.

Collapse
dtetreau profile image
David Tetreau Author • Edited

Thank you for the feedback! I added a section to the next post after I saw your recommendation. Thanks again!

Collapse
adetolayemi profile image
ADETOLAYEMI

Thank you Mr David for this effort. Your lesson is the first among many that held me down to the end! My thirst for more lessons from you needs to be quenched sir. When are you going to release more lessons?

Collapse
dtetreau profile image
David Tetreau Author

I post every day! Thanks for the kind words!

Forem Open with the Forem app