operators javascript code example

Example 1: jquery get version

jQuery().jquery;

Example 2: or operator in javascript

//The OR operator in Javascript is 2 verticals lines: ||

var a = true;
var b = false;

if(a || b) {
	//one of them is true, code inside this block will be executed
}

Example 3: javascript operators

Operator	Example	Same As
=	x = y	x = y
+=	x += y	x = x + y
-=	x -= y	x = x - y
*=	x *= y	x = x * y
/=	x /= y	x = x / y
%=	x %= y	x = x % y
**=	x **= y	x = x ** y

Example 4: less than equal to in javascript

| <= | less than or equal to |	x <= 8 | true |

Example 5: javascript ... operator

//The operator ... is part of the array destructuring.
//It's used to extract info from arrays to single variables.
//The operator ... means "the rest of the array".
var [head, ...tail] = ["Hello", "I" , "am", "Sarah"];
console.log(head);//"Hello"
console.log(tail);//["I", "am", "Sarah"]

//It can be used to pass an array as a list of function arguments
let a = [2,3,4];
Math.max(a) //--> NaN
Math.max(...a) //--> 4

Example 6: and operator in javascript

//&& returns true if both values are true
//or returns the second argument if the first is true
var a = true
var b = ""
var c = 1

true && "" //""
"" && 1 //""
false && 5 //false