JavaScript Operators
JavaScript Assignment
The assignment operator (=
) is used to give a value to a variable.
JavaScript Addition
The plus sign, known as the Addition Operator (+
), is used to add numbers.
JavaScript Multiplication
The Multiplication Operator represented by (*
) is used to multiply numbers.
Types of JavaScript Operators
There exist various kinds of JavaScript operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- String Operators
- Logical Operators
- Bitwise Operators
- Ternary Operators
- Type Operators
JavaScript Arithmetic Operators
Arithmetic Operators help us do math with numbers:
Operator |
Description |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
** |
Exponentiation (ES2016) |
/ |
Division |
% |
Modulus (Division Remainder) |
++ |
Increment |
-- |
Decrement |
Note :
Arithmetic operators are fully described in the
JS Arithmetic chapter.
JavaScript Assignment Operators
Assignment operators are like tools that give values to JavaScript variables.
The Addition Assignment Operator (+ =
) is used to add a value to a variable.
Operator |
Example |
Same As |
= |
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 :
Assignment operators are fully described in the
JS Assignment chapter.
JavaScript Comparison Operators
Operator |
Description |
== |
equal to |
=== |
equal value and equal type |
!= |
not equal |
!== |
not equal value or not equal type |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
? |
ternary operator |
Note :
Comparison operators are fully described in the
JS Comparisons chapter.
JavaScript String Comparison
You can use the same comparison operators for strings as well.
Note that strings are compared alphabetically:
JavaScript String Addition
You can use the +
sign to join or connect strings together.
The + =
assignment operator can also be used to add (concatenate) strings:
Note :
When used on strings, the + operator is called the concatenation operator.
Adding Strings and Numbers
When you add two numbers together, you get their total. But if you add a number and a string, you'll get a string as the result.
Note : If you mix a number with a word, you'll get a word as the answer!
JavaScript Logical Operators
Operator |
Description |
&& |
logical and |
|| |
logical or |
! |
logical not |
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The = Operator</h2>
<p id="demo"></p>
<script>
let x = 10;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The + Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The * Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Arithmetic Operations</h2>
<p>A normal math calculation uses two numbers (or expressions) to make a new number.</p>
<p id="demo"></p>
<script>
let a = 3;
let x = (100 + 50) * a;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The += Operator</h2>
<p id="demo"></p>
<script>
var x = 10;
x += 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<p>All comparison signs work for both numbers and words.</p>
<p id="demo"></p>
<script>
let text1 = "A";
let text2 = "B";
let result = text1 < text2;
document.getElementById("demo").innerHTML = "Is A less than B? " + result;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<p>Note that strings are compared alphabetically:</p>
<p id="demo"></p>
<script>
let text1 = "20";
let text2 = "5";
let result = text1 < text2;
document.getElementById("demo").innerHTML = "Is 20 less than 5? " + result;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<h2>The + Operator</h2>
<p>The + operator concatenates (adds) strings.</p>
<p id="demo"></p>
<script>
let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;
document.getElementById("demo").innerHTML = text3;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<h2>The += Operator</h2>
<p>The assignment operator += can concatenate strings.</p>
<p id="demo"></p>
<script>
let text1 = "What a very ";
text1 += "nice day";
document.getElementById("demo").innerHTML = text1;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<h2>The + Operator</h2>
<p>Adding a number and a string, returns a string.</p>
<p id="demo"></p>
<script>
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
document.getElementById("demo").innerHTML = x + " <br> " + y + " <br> " + z;
</script>
</body>
</html>