D Dev Notebook

JavaScript Operators

JS

JavaScript operators are symbols or keywords used to perform operations on variables and values.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Conditional (Ternary) Operator
  6. Type Operators

1. Arithmetic Operators

Arithmetic operators in JavaScript are used to perform basic mathematical operations, such as addition, subtraction, multiplication, and more.

A. Addition (+)

let a = 10;	
let b = 5;
let sum = a + b;
console.log(sum);

let firstName = "Sunday";
let lastName = "Academy";
let result = firstName + " " + lastName ;
console.log(result);

B. Subtraction(-)

let a = 10;
let b = 3;
let difference = a - b;
console.log(difference)

C. Multiplication (*)

let productPrice = 800;
let productQty = 5;
let totalPrice = productPrice * productQty ;
console.log(totalPrice);

D. Division (/)

let a = 20;
let b = 4;
let result = a / b;
console.log(result );

E. Modulus (%)

let a = 10;
let b = 3;
let result = a % b;
console.log(result );

F. Increment (++)

Pre-increment: a is incremented before it is assigned to b

let a = 10;		
let b = ++a		// a is 11, 	b is 11

Post-increment: a is incremented after it is assigned to b

let a = 10;		
let b = a++		// a is 11, 	b is 10	

G. Decresument (--)

Pre-decrement: a is decremented before it is assigned to b

let a = 10;		
let b =  --a	// a is 9, 	b is 9

Post-decrement: a is decremented after it is assigned to b

let a = 10;		
let b = a--		// a is 9, 	b is 10	

2. Assignment Operators

Assignment operators are symbols used in programming to assign values to variables.

= Assign

10+10 = 20

+= Add and assign

var a=10; a += 20; 	Now a = 30

-= Subtract and assign

var a=20; a -= 10; 	Now a = 10

*= Multiply and assign

var a=10; a *= 20; 	Now a = 200

/= Divide and assign

var a=10; a /= 2; 		Now a = 5

%= Modulus and assign

var a=10; a %= 2; 		Now a = 0

3. Comparison Operators

Comparison operators in JavaScript are used to compare two values and return true or false.

== Is equal to

10 == 20 	= false

=== Identical (equal and of same type)

10 === 20 	= false

!= Not equal to

10 != 20 	= true

!== Not Identical

20 !== 20 	= false

> Greater than

20 > 10 	= true

>= Greater than or equal to

20 >= 10 	= true

< Less than

20 < 10 	= false

<= Less than or equal to

20 <= 10 	= false

4. Logical (relational) Operators

Logical (Relational) Operators are used in programming to compare two values or expressions and determine the relationship between them. These operators return boolean values: True or False.

&& Logical AND

(10==20 && 20==33) = false

|| Logical OR

(10==20 || 20==33) = false

! Logical Not

!(10==20) = true

5. Conditional (ternary) Operators

Conditional (Ternary) Operators allow you to evaluate a condition in a concise, single-line format. These operators are used to decide which value or expression to return based on a given condition. They are essentially shorthand for an if-else statement.

? : Conditional

condition ? value if true : value if false

(22 < 18) ? "Too young":"Old enough";

6. Type Operator

Type operators are used in programming to determine or manipulate the type of a variable.

Returns the type of a variable

let myName = 'Digital Hero'
typeof(myName)

On this page