JavaScript Operators

Operators in JavaScript are symbols that perform operations on operands. They are crucial for manipulating and evaluating values. Understanding and effectively using these operators is fundamental in JavaScript programming. This tutorial covers various types of operators in JavaScript, including arithmetic, assignment, comparison, logical, and more.

 

Table of Contents

Introduction to Operators

Arithmetic Operators

Assignment Operators

Comparison Operators

Logical Operators

Bitwise Operators

Unary Operators

Ternary (Conditional) Operator

Operator Precedence

Conclusion

 

Introduction to Operators

Operators are symbols that perform operations on variables or values. JavaScript supports various types of operators that allow for actions like arithmetic calculations, assignment, comparisons, logical operations, and more.

 

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric values.

Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus (Remainder): %
Exponentiation: *

let a = 10;
let b = 5;
let sum = a + b; // sum = 15
let product = a * b; // product = 50

 

Assignment Operators

Assignment operators are used to assign values to variables.

=
+=
-=
*=
/=
%=

let x = 10;
x += 5; // Equivalent to x = x + 5 (x is now 15)

 

Comparison Operators

Comparison operators are used to compare values and return a boolean result.

Equal to: ==
Not equal to: !=
Strict equal to: ===
Strict not equal to: !==
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=

let num1 = 10;
let num2 = 5;
console.log(num1 > num2); // true

 

Logical Operators

Logical operators are used to combine or negate logical values.

Logical AND: &&
Logical OR: ||
Logical NOT: !

let isTrue = true;
let isFalse = false;
console.log(isTrue && isFalse); // false

 

Bitwise Operators

Bitwise operators perform operations at the bit level.

Bitwise AND: &
Bitwise OR: |
Bitwise XOR: ^
Bitwise NOT: ~
Left shift: <<
Sign-propagating right shift: >>
Zero-fill right shift: >>>

let a = 5; // Binary representation: 101
let b = 3; // Binary representation: 011
console.log(a & b); // Bitwise AND (result: 1)

 

Unary Operators

Unary operators are used on a single operand.

Increment: ++
Decrement: --
Unary plus: +
Unary minus: -

let num = 5;
num++; // Increment by 1 (num is now 6)

 

Ternary (Conditional) Operator

The ternary operator is a shorthand for an if-else statement.

let age = 18;
let message = (age >= 18) ? "You are an adult" : "You are a minor";

 

Operator Precedence

Operators in JavaScript have different levels of precedence. Understanding this order helps in evaluating expressions correctly.

let result = 10 + 5 * 2; // result = 20 (Multiplication has higher precedence)

 

Conclusion

Operators in JavaScript are powerful tools for performing various operations on data. This tutorial has provided an overview of different types of operators and their functionalities. Mastering these operators is crucial for writing efficient, concise, and accurate JavaScript code. Experiment with these operators in your code to gain a deeper understanding of their functionalities and usage in JavaScript programming.
 

© 2022-2023 All rights reserved.