Skip to content

Latest commit

 

History

History
192 lines (132 loc) · 2.89 KB

File metadata and controls

192 lines (132 loc) · 2.89 KB

Operators


Operators

The typeof Operator

typeof 42;              // "number"
typeof 1.4142135;       // "number"
typeof 'hello';         // "string"
typeof 'false';         // "string"
typeof false;           // "boolean"
typeof true;            // "boolean"
typeof {key: 'value' }; // "object"
typeof function () {};  // "function"
typeof Math.sin;        // "function"
typeof null;            // "object"
typeof undefined;       // "undefined"

Operators

Unary Operators

++value  // increments value by 1 before the statement is evaluated 
value++  // increments value by 1 after the statement is evaluated 
--value  // decremented value by 1 before the statement is evaluated 
value--  // decremented value by 1 after the statement is evaluated 

Operators

Logical Operators

!value  // negate the boolean value
a && b  // returns the logical AND of a and b 
a || b  // returns the logical OR of a and b

Operators precedence

! is executed first, then comes && and finally ||

Use parentheses instead of relying on operator precedence,
in order to make your code easier to read and understand.

false && false || true && true;     // true
(false && false) || (true && true); // true

Operators

Basic operation

a + b // returns the sum of its operands
a - b // returns the difference of its operands
a * b // multiply two numbers
a / b // divides the first operand by the second operand
a % b // returns the remainder of the division 

Operators

Relational Operators

a < b  // return true if a is less-than b, false otherwise
a > b  // return true if a is greater-than b, false otherwise
a <= b // return true if a is less-than-or-equal-to b, false otherwise
a >= b // return true if a is greater-than-or-equal-to b, false otherwise 

Operators

Equality operators

a === b // returns true if a is equals to b, false otherwise
a !== b // returns true if a is not equals to b, false otherwise

Operators

Conditional Operator

conditional_expression ? true_case : false_case;

if conditional_expression is true
then returns true_case value
else returns false_case value

var a = 5;
var b = 6;
var max = (a > b) ? a : b; // max is 6

Operators

Assignment Operators

variable = value // assigns value to variable

Operators

Assignment Operators

variable += value // variable = variable + value
variable -= value // variable = variable - value
variable *= value // variable = variable * value
variable /= value // variable = variable / value
variable %= value // variable = variable % value