js operators code example
Example 1: js logical operators
Javascript Logical Operators
&& Logical and
|| Logical or
! Logical not
Example 2: or operator in javascript
var a = true;
var b = false;
if(a || b) {
}
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: javascript ... operator
var [head, ...tail] = ["Hello", "I" , "am", "Sarah"];
console.log(head);
console.log(tail);
let a = [2,3,4];
Math.max(a)
Math.max(...a)
Example 5: javascript ... operator
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
console.log(sum.apply(null, numbers));
function sum(...numbers) {
let sum = 0;
for (const number of numbers)
sum += number;
return sum;
}
console.log(sum(1, 2, 3, 4, 5));
console.log(sum(...[4, 5, ...numbers]));
Example 6: js comparison operators
Javascript Comparison Operators
== Equal to
=== Equal value and equal type
!= Not equal
!== Not equal value or not equal type
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
? Ternary operator