Wednesday, April 22, 2015

JavaScript Operators Reference

JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.

JavaScript Arithmetic Operators :

Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5, the table below explains the arithmetic operators:


OperatorDescriptionExampleResult in yResult in x
+Additionx = y + 2y = 5x = 7
-Subtractionx = y - 2y = 5x = 3
*Multiplicationx = y * 2y = 5x = 10
/Divisionx = y / 2y = 5x = 2.5
%Modulus (division remainder)x = y % 2y = 5x = 1
++Incrementx = ++yy = 6x = 6
x = y++y = 6x = 5
--Decrementx = --yy = 4x = 4
x = y--y = 4x = 5


JavaScript Assignment Operators :

Assignment operators are used to assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
OperatorExampleSame AsResult in x
=x = yx = yx = 5
+=x += yx = x + yx = 15
-=x -= yx = x - yx = 5
*=x *= yx = x * yx = 50
/=x /= yx = x / yx = 2
%=x %= yx = x % yx = 0
For a tutorial about assignment operators, read our JavaScript Assignment Tutorial.

JavaScript String Operators :

The + operator, and the += operator can also be used to concatenate (add) strings.
Given that text1 = "Good "text2 = "Morning"and text3 = "", the table below explains the operators:
OperatorExampletext1text2text3
+text3 = text1 + text2"Good ""Morning" "Good Morning"
+=text1 += text2"Good ""Morning"""

Comparison Operators :

Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x = 5, the table below explains the comparison operators:
OperatorDescriptionComparingReturns
==equal tox == 8false
x == 5true
===equal value and equal typex === "5"false
x === 5true
!=not equalx != 8true
!==not equal value or not equal typex !== "5"true
x !== 5false
>greater thanx > 8false
<less thanx < 8true
>=greater than or equal tox >= 8false
<=less than or equal tox <= 8true


Conditional (Ternary) Operator :

The conditional operator assigns a value to a variable based on a condition.
SyntaxExample
variablename = (condition) ? value1:value2voteable = (age < 18) ? "Too young":"Old enough";
Example explained: If the variable "age" is a value below 18, the value of the variable "voteable" will be "Too young", otherwise the value of voteable will be "Old enough".

Logical Operators :

Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
OperatorDescriptionExample
&&and(x < 10 && y > 1) is true
||or(x === 5 || y === 5) is false
!not!(x === y) is true

JavaScript Bitwise Operators :

Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.
OperatorDescriptionExampleSame asResultDecimal
&ANDx = 5 & 10101 & 00010001 1
|ORx = 5 | 10101 | 00010101 5
~NOTx = ~ 5 ~01011010 10
^XORx = 5 ^ 10101 ^ 00010100 4
<<Left shiftx = 5 << 10101 << 11010 10
>>Right shiftx = 5 >> 10101 >> 10010  2

0 comments:

Post a Comment

Thanks For Coming The Page.