HTML CSS JAVASCRIPT PYTHON JAVA

JavaScript Operators

JavaScript operators are used to perform operations on variables and values. They allow you to perform calculations, assign values, and compare data.

Common JavaScript Operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

Addition Operator (+)

The addition operator is used to add two values.

Example
 <!DOCTYPE html>
<html>
<body>   
<p id="add"></p>

<script>
let x = 10;
let y = 5;
document.getElementById('add').innerHTML = x + y;
</script>
</body>
</html>
Try this code

Subtraction Operator (-)

The subtraction operator is used to subtract one value from another.

let x = 10;
let y = 5;
document.getElementById('sub').innerHTML = x - y;

Multiplication Operator (*)

The multiplication operator is used to multiply values.

let x = 10;
let y = 5;
document.getElementById('mul').innerHTML = x * y;

Division Operator (/)

The division operator is used to divide one value by another.

let x = 10;
let y = 5;
document.getElementById('div').innerHTML = x / y;

Assignment Operators

Assignment operators are used to assign values to variables. They can also be used to perform operations and assign the result.

Assignment Operator (=)

The assignment operator is used to assign a value to a variable.

Example
<!DOCTYPE html>
<html>
<body>   
<p id="assign"></p>

<script>
let x;
x = 10;
document.getElementById('assign').innerHTML = x;
</script>
</body>
</html>
Try this code

Addition Assignment Operator (+=)

This operator adds a value to a variable and assigns the result.

Example
<!DOCTYPE html>
<html>
<body> 
<p id="addAssign"></p>

<script>
let x = 10;
x += 5;
document.getElementById('addAssign').innerHTML = x;
</script>
</body>
</html> 
Try this code

Subtraction Assignment Operator (-=)

This operator subtracts a value from a variable and assigns the result.

let x = 10;
x -= 5;
document.getElementById('subAssign').innerHTML = x;

Comparison Operators

Comparison operators are used to compare two values. They return either true or false.

Equal to (==)

This operator checks if two values are equal.

Example
<!DOCTYPE html>
<html>
<body> 
<p id="eq"></p>

<script>
let x = 10;
let y = '10';
document.getElementById('eq').innerHTML = (x == y);
</script>
</body>
</html>
Try this code

Strict Equal (===)

This operator checks both value and data type.

Example
<!DOCTYPE html>
<html>
<body> 

<p id="seq"></p>
<script>
let x = 10;
let y = '10';
document.getElementById('seq').innerHTML = (x === y)
</script>
</body>
</html> 
Try this code

Not Equal (!=)

This operator checks if two values are not equal.

Example
<!DOCTYPE html>
<html>
<body>    
<p id="neq"></p>

<script>
let x = 10;
let y = 5;
document.getElementById('neq').innerHTML = (x != y);
</script>
</body>
</html>
Try this code

Greater Than (>)

Checks if one value is greater than another.

Example
<!DOCTYPE html>
<html>
<body>  
<p id="gt"></p>

<script>
let x = 10;
let y = 5;
document.getElementById('gt').innerHTML = (x > y);
</script>
</body>
</html> 
Try this code

Less Than (<)

Checks if one value is less than another.

Example
<!DOCTYPE html>
<html>
<body>  
<p id="lt"></p>

<script>
let x = 5;
let y = 10;
document.getElementById('lt').innerHTML = (x < y);
</script>
</body>
</html>  
Try this code

Why Operators are Important

Best Practices