0% found this document useful (0 votes)
2 views10 pages

Java Script Operators

This document provides an overview of various JavaScript operators, including arithmetic, assignment, comparison, logical, bitwise, string concatenation, and miscellaneous operators. Each operator type is explained with examples demonstrating their usage in JavaScript. The document serves as a tutorial for understanding how to perform different operations using these operators.

Uploaded by

Sripathi Ravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Java Script Operators

This document provides an overview of various JavaScript operators, including arithmetic, assignment, comparison, logical, bitwise, string concatenation, and miscellaneous operators. Each operator type is explained with examples demonstrating their usage in JavaScript. The document serves as a tutorial for understanding how to perform different operations using these operators.

Uploaded by

Sripathi Ravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Script Operators

JavaScript Operator Types


Here is a list of different JavaScript operators you will learn in this tutorial:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. String Operators
7. Miscellaneous Operators

1. JavaScript Arithmetic Operators


We use arithmetic operators to perform arithmetic calculations like addition,
subtraction, etc. For example,
5 - 3; // 2

Here, we used the - operator to subtract 3 from 5.


Commonly Used Arithmetic Operators

Operator Name Example

+ Addition 3 + 4 // 7

- Subtraction 5 - 3 // 2

* Multiplication 2 * 3 // 6

/ Division 4 / 2 // 2

% Remainder 5 % 2 // 1

++5 or 5++ //
++ Increment (increments by 1)
6

--4 or 4-- //
-- Decrement (decrements by 1)
3

** Exponentiation (Power) 4 ** 2 // 16
Example 1: Arithmetic Operators in JavaScriptlet x = 5;

// addition operator
console.log("Addition: x + 3 = ", x + 3);

// subtraction operator
console.log("Subtraction: x - 3 =", x - 3);

// multiplication operator
console.log("Multiplication: x * 3 =", x * 3);

// division operator
console.log("Division: x / 3 =", x / 3);

// remainder operator
console.log("Remainder: x % 3 =", x % 3);

// increment operator
console.log("Increment: ++x =", ++x);

// decrement operator
console.log("Decrement: --x =", --x);

// exponentiation operator
console.log("Exponentiation: x ** 3 =", x ** 3);

Output

Addition: x + 3 = 8
Subtraction: x - 3 = 2
Multiplication: x * 3 = 15
Division: x / 3 = 1.6666666666666667
Remainder: x % 3 = 2
Increment: ++x = 6
Decrement: --x = 5
Exponentiation: x ** 3 = 125

Note: The increment operator ++ adds 1 to the operand. And, the decrement
operator -- decreases the value of the operand by 1.
To learn more, visit Increment ++ and Decrement -- Operators.
2. JavaScript Assignment Operators
We use assignment operators to assign values to variables. For example,
let x = 5;

Here, we used the = operator to assign the value 5 to the variable x.


Commonly Used Assignment Operators

Operato
Name Example
r

= Assignment Operator a = 7;

+= Addition Assignment a += 5; // a = a + 5

-= Subtraction Assignment a -= 2; // a = a - 2

Multiplication
*= a *= 3; // a = a * 3
Assignment

/= Division Assignment a /= 2; // a = a / 2

%= Remainder Assignment a %= 2; // a = a % 2

Exponentiation
**= a **= 2; // a = a**2
Assignment

Example 2: Assignment Operators in JavaScript


// assignment operator
let a = 7;
console.log("Assignment: a = 7, a =", a);

// addition assignment operator


a += 5; // a = a + 5
console.log("Addition Assignment: a += 5, a =", a);

// subtraction assignment operator


a -= 5; // a = a - 5
console.log("Subtraction Assignment: a -= 5, a =", a);

// multiplication assignment operator


a *= 2; // a = a * 2
console.log("Multiplication Assignment: a *= 2, a =", a);

// division assignment operator


a /= 2; // a = a / 2
console.log("Division Assignment: a /= 2, a =", a);

// remainder assignment operator


a %= 2; // a = a % 2
console.log("Remainder Assignment: a %= 2, a =", a);

// exponentiation assignment operator


a **= 2; // a = a**2
console.log("Exponentiation Assignment: a **= 7, a =", a);

Output
Assignment: a = 7, a = 7
Addition Assignment: a += 5, a = 12
Subtraction Assignment: a -= 5, a = 7
Multiplication Assignment: a *= 2, a = 14
Division Assignment: a /= 2, a = 7
Remainder Assignment: a %= 2, a = 1
Exponentiation Assignment: a **= 7, a = 1

3. JavaScript Comparison Operators


We use comparison operators to compare two values and return a boolean value
(true or false). For example,

const a = 3, b = 2;
console.log(a > b);

// Output: true

Here, we have used the > comparison operator to check whether a (whose value
is 3) is greater than b (whose value is 2).
Since 3 is greater than 2, we get true as output.

Note: In the above example, a > b is called a boolean expression since


evaluating it results in a boolean value.
Commonly Used Comparison Operators

Operato
Meaning Example
r

3 == 5 gives us
== Equal to
false

!= Not equal to 3 != 4 gives us true

> Greater than 4 > 4 gives us false

< Less than 3 < 3 gives us false

Greater than or equal


>= 4 >= 4 gives us true
to

<= Less than or equal to 3 <= 3 gives us true

3 === "3" gives us


=== Strictly equal to
false

3 !== "3" gives us


!== Strictly not equal to
true

Example 3: Comparison Operators in JavaScript


// equal to operator
console.log("Equal to: 2 == 2 is", 2 == 2);

// not equal operator


console.log("Not equal to: 3 != 3 is", 3 != 3);

// strictly equal to operator


console.log("Strictly equal to: 2 === '2' is", 2 === '2');

// strictly not equal to operator


console.log("Strictly not equal to: 2 !== '2' is", 2 !== '2');
// greater than operator
console.log("Greater than: 3 > 3 is", 3 > 3);

// less than operator


console.log("Less than: 2 > 2 is", 2 > 2);

// greater than or equal to operator


console.log("Greater than or equal to: 3 >= 3 is", 3 >= 3);

// less than or equal to operator


console.log("Less than or equal to: 2 <= 2 is", 2 <= 2);

Output
Equal to: 2 == 2 is true
Not equal to: 3 != 3 is false
Strictly equal to: 2 === '2' is false
Strictly not equal to: 2 !== '2' is true
Greater than: 3 > 3 is false
Less than: 2 > 2 is false
Greater than or equal to: 3 >= 3 is true
Less than or equal to: 2 <= 2 is true
Difference between equality (== and !=) and strict equality (=== and !==) operators.

4. JavaScript Logical Operators


We use logical operators to perform logical operations on boolean expressions. For
example,

const x = 5, y = 3;
console.log((x < 6) && (y < 5));

// Output: true

Here, && is the logical operator AND. Since both x < 6 and y < 5 are true, the
combined result is true.
Commonly Used Logical Operators

Operator Syntax Description

true only if both


&& expression1 && expression1 and
(Logical
expression2 expression2 are
AND)
true

true if either
|| expression1 || expression1 or
(Logical
expression2 expression2 is
OR)
true

! false if expression
(Logical !expression
is true and vice versa
NOT)

Example 4: Logical Operators in JavaScript


let x = 3;

// logical AND
console.log((x < 5) && (x > 0)); // true
console.log((x < 5) && (x > 6)); // false

// logical OR
console.log((x > 2) || (x > 5)); // true
console.log((x > 3) || (x < 0)); // false

// logical NOT
console.log(!(x == 3)); // false
console.log(!(x < 2)); // true
5. JavaScript Bitwise Operators
We use bitwise operators to perform binary operations on integers.

Operator Description Example

& Bitwise AND 5 & 3 // 1

| Bitwise OR 5 | 3 // 7

^ Bitwise XOR 5 ^ 3 // 6

~ Bitwise NOT ~5 // -6

<< Left shift 5 << 1 // 10

Sign-
-10 >> 1 //
>> propagating
-5
right shift

Zero-fill -10 >>> 1 //


>>>
right shift 2147483643

Note: We rarely use bitwise operators in everyday programming. If you are interested,
visit JavaScript Bitwise Operators to learn more.
6. JavaScript String Concatenation Operator
In JavaScript, you can also use the + operator to concatenate (join) two
strings. For example,
let str1 = "Hel", str2 = "lo";
console.log(str1 + str2);

// Output: Hello

7. JavaScript Miscellaneous Operators


JavaScript has many more operators besides the ones we listed above.
You will learn about them in detail in later tutorials.

Operator Description Example

Comma: Evaluates
multiple operands
let a = (1,
, and returns the
3, 4); // 4
value of the last
operand.

(50 > 40) ?


Ternary: Returns
"pass" :
?: value based on
"fail"; //
the condition.
"pass"

Returns the data


typeof 3; //
typeof type of the
"number"
variable.
Returns true if
the specified objectX
instanceof object is a valid instanceof
object of the ClassX
specified class.

Discards any
void(x) //
void expression's
undefined
return value.

You might also like