i

JavaScript

Logical Operator

&&, || and ! are logical operators, and they can be applied to values of any type.

The below table explains the operators, its usage along with an example.

Operator

Description

Example

Return value

&&

and – returns true if the conditions are true, false otherwise

var x = 9;

var y = 3;

x < 20 && y > 5

false

||

or – returns true if one of the conditions is true, false otherwise

var x = 9;

var y = 3;

x < 20 || y > 5

true

!

not

var x = 9;

var y = 3;

!(x == y)

true

 

Ternary Operator:

“?” is the ternary operator that is used to assign a value to a variable based on the truthy value of the condition.

Syntax:

variable = (condition) ? value1 : value2

If the condition evaluates to true, variable will be assigned the value , value1 else it will be assigned the value, value2.

Example:

var a = 10;

var b = (a > 5) ? 1 : 0;

The value of variable ‘b’ is 1, since a > 5 is true.