JavaScript Operators
JavaScript Operators
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
JavaScript Operators
❮ Previous Next ❯
Javascript operators are used to perform different types of mathematical and logical
computations.
Examples:
JavaScript Assignment
The Assignment Operator ( = ) assigns a value to a variable:
Assignment Examples
let x = 10;
Try it Yourself
Tutorials » Exercises Services Sign Up Log in
//
HTML CSS the
Assign JAVASCRIPT
value 5 toSQL
x PYTHON JAVA PHP HOW TO W3.CSS C
let x = 5;
// Assign the value 2 to y
let y = 2;
// Assign the value x + y to z:
let z = x + y;
Try it Yourself »
JavaScript Addition
The Addition Operator ( + ) adds numbers:
Adding
let x = 5;
let y = 2;
let z = x + y;
Try it Yourself »
JavaScript Multiplication
The Multiplication Operator ( * ) multiplies numbers:
Multiplying
let x = 5;
let y = 2;
let z = x * y;
Try it Yourself »
Types ofJavaScript
Tutorials
Exercises
Operators
Services Sign Up Log in
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
There are different types of JavaScript operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
String Operators
Logical Operators
Bitwise Operators
Ternary Operators
Type Operators
Try it Yourself »
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
++ Increment
--
Tutorials Decrement
Exercises Services Sign Up Log in
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Note
Arithmetic operators are fully described in the JS Arithmetic chapter.
ADVERTISEMENT
Assignment
let x = 10;
x += 5;
Try it Yourself »
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
Note Tutorials Exercises Services Sign Up Log in
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Assignment operators are fully described in the JS Assignment chapter.
== equal to
!= not equal
? ternary operator
Note
Comparison operators are fully described in the JS Comparisons chapter.
Example
Tutorials
let text1 = "A";
let text2 = "B";
Exercises Services Sign Up Log in
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
Example
Tutorials Exercises
let text1 = "What a very ";
text1 += "nice day";
Services Sign Up Log in
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
The result of text1 will be:
Try it Yourself »
Note
When used on strings, the + operator is called the concatenation operator.
Adding a number and a string, will return the sum as a concatenated string like 5 + "5" = "55".
Example
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
10
55
Hello5
Try it Yourself »
Note Tutorials Exercises Services Sign Up Log in
|| logical or
! logical not
Note
Logical operators are fully described in the JS Comparisons chapter.
Note
Type operators are fully described in the JS Type Conversion chapter.
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.
Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010
?
Exercise
Consider the following code:
let x = 5;
let y = '8';
let z = x + y;
undefined
58