search ]

Understanding JavaScript Operators

JavaScript operators are symbols used to perform operations on variables and values. They are the backbone of any logic and arithmetic in your scripts. This post will explore different types of JavaScript operators with examples to illustrate their use.

Arithmetic Operators

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

OperatorDescriptionExample
+Additionlet sum = 10 + 5; // 15
Subtractionlet difference = 10 – 5; // 5
*Multiplicationlet product = 10 * 5; // 50
/Divisionlet quotient = 10 / 5; // 2
%Moduluslet remainder = 10 % 3; // 1

More Information

1. Addition (+)

Adds two values.

let sum = 10 + 5; // 15
    

2. Subtraction (-)

Subtracts one value from another.

let difference = 10 - 5; // 5
    

3. Multiplication (*)

Multiplies two values.

let product = 10 * 5; // 50
    

4. Division (/)

Divides one value by another.

let quotient = 10 / 5; // 2
    

5. Modulus (%)

Returns the remainder of a division.

let remainder = 10 % 3; // 1
    

Assignment Operators

Assignment operators assign values to variables, allowing you to set or update the value of a variable.

OperatorDescriptionExample
=Assignmentlet x = 10;
+=Addition Assignmentx += 5; // x = x + 5; // 15
-=Subtraction Assignmentx -= 3; // x = x – 3; // 7
*=Multiplication Assignmentx *= 2; // x = x * 2; // 20
/=Division Assignmentx /= 2; // x = x / 2; // 5

More Information

1. Assignment (=)

Assigns a value to a variable.

let x = 10;
    

2. Addition Assignment (+=)

Adds and assigns a value to a variable.

x += 5; // x = x + 5; // 15
    

3. Subtraction Assignment (-=)

Subtracts and assigns a value to a variable.

x -= 3; // x = x - 3; // 7
    

4. Multiplication Assignment (*=)

Multiplies and assigns a value to a variable.

x *= 2; // x = x * 2; // 20
    

5. Division Assignment (/=)

Divides and assigns a value to a variable.

x /= 2; // x = x / 2; // 5
    

Comparison Operators

Comparison operators compare two values and return a boolean result, allowing you to evaluate conditions in your code.

OperatorDescriptionExample
==Equal tolet isEqual = (5 == ‘5’); // true
===Strict Equal tolet isStrictEqual = (5 === ‘5’); // false
!=Not Equal tolet isNotEqual = (5 != ‘5’); // false
!==Strict Not Equal tolet isStrictNotEqual = (5 !== ‘5’); // true
>Greater thanlet isGreater = (10 > 5); // true
<Less thanlet isLess = (10 < 5); // false
>=Greater than or equal tolet isGreaterOrEqual = (10 >= 10); // true
<=Less than or equal tolet isLessOrEqual = (10 <= 5); // false

More Information

1. Equal to (==)

Checks if two values are equal.

let isEqual = (5 == '5'); // true
    

2. Strict Equal to (===)

Checks if two values and their types are equal.

let isStrictEqual = (5 === '5'); // false
    

3. Not Equal to (!=)

Checks if two values are not equal.

let isNotEqual = (5 != '5'); // false
    

4. Strict Not Equal to (!==)

Checks if two values and their types are not equal.

let isStrictNotEqual = (5 !== '5'); // true
    

5. Greater than (>)

Checks if one value is greater than another.

let isGreater = (10 > 5); // true
    

6. Less than (<)

Checks if one value is less than another.

let isLess = (10 < 5); // false
    

7. Greater than or equal to (>=)

Checks if one value is greater than or equal to another.

let isGreaterOrEqual = (10 >= 10); // true
    

8. Less than or equal to (<=)

Checks if one value is less than or equal to another.

let isLessOrEqual = (10 <= 5); // false
    

Logical Operators

Logical operators are used to combine multiple boolean expressions, enabling complex condition evaluations.

OperatorDescriptionExample
&&Logical ANDlet andResult = (true && false); // false
||Logical ORlet orResult = (true || false); // true
!Logical NOTlet notResult = !true; // false

More Information

1. Logical AND (&&)

Returns true if both expressions are true.

let andResult = (true && false); // false
    

2. Logical OR (||)

Returns true if either expression is true.

let orResult = (true || false); // true
    

3. Logical NOT (!)

Inverts the boolean value of an expression.

let notResult = !true; // false
    

Conclusion

JavaScript operators are essential tools for performing various operations in your code. Understanding and using them correctly will help you write more efficient and readable scripts. Experiment with these operators to see how they work and integrate them into your projects.

Roee Yossef
Roee Yossef

I develop custom WordPress themes by design. I love typography, colors & everything between, and aim to provide high performance, seo optimized websites with a clean & semantic code.

0 Comments...

Leave a Comment

Add code using the buttons below. For example, to add PHP click the PHP button & add the code inside the shortcode. Typo? Please let us know...