Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
89 views

Javascript Arithmetic Operators

JavaScript operators include arithmetic, comparison, logical, assignment, conditional, and other types. Operators are symbols that perform operations on operands to assign values, compare values, or perform calculations. Operator precedence and associativity determine the order that operations are performed in expressions. Common operators include +, -, *, / for arithmetic, <, >, ==, != for comparisons, &&, ||, ! for logic, and = for assignment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Javascript Arithmetic Operators

JavaScript operators include arithmetic, comparison, logical, assignment, conditional, and other types. Operators are symbols that perform operations on operands to assign values, compare values, or perform calculations. Operator precedence and associativity determine the order that operations are performed in expressions. Common operators include +, -, *, / for arithmetic, <, >, ==, != for comparisons, &&, ||, ! for logic, and = for assignment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JavaScript operators are symbols which are used to assign values, compare values, perform arithmetic operations,

and more. 

 The variables (operations) are called operands.


 The operation (to be performed between the two operands) is defined by an operator.
JavaScript supports the following types of operators.

 Arithmetic Operators
 Comparison Operators
 Logical (or Relational) Operators
 Assignment Operators
 Conditional (or ternary) Operators
 String Operators
 Type Operators
 Bitwise Operators
JavaScript Arithmetic Operators –
Arithmetic operators perform arithmetic operations on numbers.

Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
— Decrement
Example –

1var x = 100 + 50;

1 // Addition
2 var a = 4;
3 var b = 3;
4 var x = a + b; // adding 2 variables
5 var x = (100 + 50) * a; // expressions

7 // Subtraction
8 var x = 5;
9 var y = 2;
10var z = x - y;
11 
12// Multiplication
13var x = 5;
14var y = 2;
15var z = x * y;
16 
17// Division
18var x = 5;
19var y = 2;
20var z = x / y;
21 
22// Modulo
23var x = 5;
24var y = 2;
25var z = x % y;
26 
27// Increment
28var x = 5;
29x++;
30var z = x;
31 
32// Decrement
33var x = 5;
34x--;
35var z = x;

Associativity –
Associativity determines the way in which operators of the same precedence are parsed. For example, consider an
expression:

1a OP b OP c // OP means a operator
Left-associativity (left-to-right) means that it is processed as (a OP b) OP c, while right-associativity (right-to-left)
means it is interpreted as a OP (b OP c).

Operator Precedence –
Operator precedence describes the order in which operations are performed in an arithmetic expression.

Example –

1var x = 100 + 50 * 3;
As in traditional school mathematics, the multiplication is done first. Multiplication (*) and division (/) have
higher precedence than addition (+) and subtraction (-).

Precedence Operator type Associativity Individual operators


20 Grouping n/a ( … )

19 Member Access left-to-right … . …

Computed Member Access left-to-right … [ … ]

new (with argument list) n/a new … ( … )

Function Call left-to-right … ( <var>… </var>)

18 new (without argument list) right-to-left new …

17 Postfix Increment n/a … ++

Postfix Decrement … --

16 Logical NOT right-to-left ! …

Bitwise NOT ~ …

Unary Plus + …

Unary Negation - …

Prefix Increment ++ …

Prefix Decrement -- …

typeof typeof …

void void …
delete delete …

await await …

15 Exponentiation right-to-left … ** …

14 Multiplication left-to-right … * …

Division … / …

Remainder … % …

13 Addition left-to-right … + …

Subtraction … - …

12 Bitwise Left Shift left-to-right … &lt;&lt; …

Bitwise Right Shift … &gt;&gt; …

Bitwise Unsigned Right Shift … &gt;&gt;&gt; …

11 Less Than left-to-right … &lt; …

Less Than Or Equal … &lt;= …

Greater Than … &gt; …

Greater Than Or Equal … &gt;= …

in … in …

instanceof … instanceof …

10 Equality left-to-right … == …

Inequality … != …

Strict Equality … === …

Strict Inequality … !== …

9 Bitwise AND left-to-right … &amp; …

8 Bitwise XOR left-to-right … ^ …

7 Bitwise OR left-to-right … | …

6 Logical AND left-to-right … &amp;&amp; …

5 Logical OR left-to-right … || …

4 Conditional right-to-left … ? … : …

3 Assignment right-to-left … = …

… += …

… -= …

… **= …

… *= …

… /= …
… %= …

… &lt;&lt;= …

… &gt;&gt;= …

… &gt;&gt;&gt;= …

… &amp;= …

… ^= …

… |= …

2 yield right-to-left yield …

yield* yield* …

1 Comma / Sequence left-to-right … , …

JavaScript Comparison Operators –


Comparison and Logical operators are used to test for true or false.  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:

Operator Description Comparing Returns


== equal to x == 8 false
x == 5 true
x == “5” true
=== equal value and equal type x === 5 true
x === “5” false
!= not equal x != 8 true
!== not equal value or not equal type x !== 5 false
x !== “5” true
x !== 8 true
> greater than x>8 false
< less than x<8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true
Example – 

1 if (age < 18) text = "Too young";


2 var voteable = (age < 18) ? "Too young":"Old enough"; // ternary operator
3  
4 // if -else example
5 age = Number(age);
6 if (isNaN(age)) {
7     voteable = "Input is not a number";
8 } else {
9     voteable = (age < 18) ? "Too young" : "Old enough";
10}

JavaScript Logical (or Relational) Operators –


Comparison and Logical operators are used to test for true or false.   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:

Operator Description Example


&& and (x < 10 && y > 1) is true
|| or (x == 5 || y == 5) is false
! not !(x == y) is true

JavaScript Assignment Operators –


Assignment operators assign values to JavaScript variables.

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
>>= 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
The **= operator is an experimental part of the ECMAScript 2016 proposal (ES7). It is not stable across browsers.
Do not use it.

Example –

1 // The = assignment operator assigns a value to a variable.


2 var x = 10;

4 // The += assignment operator adds a value to a variable.
5 x += 5;

7 //The -= assignment operator subtracts a value from a variable.
8 var x = 10;
9 x -= 5;
10 
11// The *= assignment operator multiplies a variable.
12var x = 10;
13x *= 5;
14 
15// The /= assignment divides a variable.
16var x = 10;
17x /= 5;
18 
19// The %= assignment operator assigns a remainder to a variable.
20var x = 10;
21x %= 5;

JavaScript Conditional (Ternary) Operator –


JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.

Syntax –

1variablename = (condition) ? value1:value2


Example –

1var voteable = (age < 18) ? "Too young":"Old enough";

JavaScript String Operators –


The + operator can also be used to add (concatenate) strings.

1var txt1 = "John";


2var txt2 = "Doe";
3var txt3 = txt1 + " " + txt2;
Output – 

1John Doe
The += assignment operator can also be used to add (concatenate) strings:

1var txt1 = "What a very ";


2txt1 += "nice day";
Output – 

1What a very nice day

Adding Strings and Numbers –


Adding two numbers, will return the sum, but adding a number and a string will return a string:

Example –

1var x = 5 + 5;
2var y = "5" + 5;
3var z = "Hello" + 5;
Output –

110
255
3Hello5

JavaScript Type Operators –


Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

JavaScript Bitwise Operators –


Operator Description Example Same as Result Decimal
& AND 5&1 0101 & 0001 0001  1
| OR 5|1 0101 | 0001 0101  5
~ NOT ~5  ~0101 1010  10
^ XOR 5^1 0101 ^ 0001 0100  4
<< Zero fill left shift 5 << 1 0101 << 1 1010  10
>> Signed right shift 5 >> 1 0101 >> 1 0010  2
>>> Zero fill right shift 5 >>> 1 0101 >>> 1 0010  2
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.

You might also like