JavaScript Made Easy: Part 4

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 t…


This content originally appeared on DEV Community and was authored by David Tetreau

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
*/

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 lessons.

const currentLesson = 4; //assignment operator

Arithmetic Operators

  • Addition

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

2 + 2;
// expected result: 4

2 + true;
// expected result: 3

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

2001 + ' is my graduation year';
// expected result: "2001 is my graduation year"

  • 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

  • Other operators that can be used to do arithmetic the same way are:
5 - 5; //subtraction
3 ** 4; /*
       exponentiation
       expected output is 81
       same as 3 to the 4th power
       */
1 / 2; //division
12 % 5; /*modulus
       returns the remainder 
       after division
       */
++; // increment (increases a number by 1)
--; // decrement (decreases a number by 1)

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.


This content originally appeared on DEV Community and was authored by David Tetreau


Print Share Comment Cite Upload Translate Updates
APA

David Tetreau | Sciencx (2021-04-29T16:53:43+00:00) JavaScript Made Easy: Part 4. Retrieved from https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/

MLA
" » JavaScript Made Easy: Part 4." David Tetreau | Sciencx - Thursday April 29, 2021, https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/
HARVARD
David Tetreau | Sciencx Thursday April 29, 2021 » JavaScript Made Easy: Part 4., viewed ,<https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/>
VANCOUVER
David Tetreau | Sciencx - » JavaScript Made Easy: Part 4. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/
CHICAGO
" » JavaScript Made Easy: Part 4." David Tetreau | Sciencx - Accessed . https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/
IEEE
" » JavaScript Made Easy: Part 4." David Tetreau | Sciencx [Online]. Available: https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/. [Accessed: ]
rf:citation
» JavaScript Made Easy: Part 4 | David Tetreau | Sciencx | https://www.scien.cx/2021/04/29/javascript-made-easy-part-4/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.