- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Arithmetic
JavaScript Arithmetic Operators
Arithmetic operators calculate with numbers (literals or variables).
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponentiation (ES2016) |
/ | Division |
% | Modulus (Remainder) |
++ | Increment |
-- | Decrement |
Arithmetic Operations
Normal arithmetic operations involve two numbers.
Both numbers can be taken literally:
Operators And Operands
The numbers involved in an arithmetic operation are referred to as operands.
An operator defines the operation (to be performed between the two operands).
Operand | Operator | Operand |
---|---|---|
100 | + | 50 |
Adding
The addition operator (+
) adds numbers:
var y = 2;
var z = x + y;
Subtracting
The operator for subtraction (-) subtracts numbers.
var y = 2;
var z = x - y;
Multiplying
The operator for multiplication (*) multiplies numbers.
var y = 2;
var z = x * y;
Dividing
The operator for division (/) divides numbers.
var y = 2;
var z = x / y;
Remainder
The modulus operator (%
) returns the division remainder.
var y = 2;
var z = x % y;
The division of two integers yields a quotient and a remainder in mathematics.
The remainder of a division is the result of a modulo operation in mathematics.
Incrementing
The increment operator (++
) increments numbers.
Example
x++;
var z = x;
Decrementing
The decrement operator (--
) decrements numbers.
Example
x--;
var z = x;
Exponentiation
The operator for exponentiation (**) raises the value of the first operand to the power of the second operand.
Example
var z = x ** 2; // result is 25
var z = Math.pow(x,2); // result is 25
Operator Precedence
Operator precedence describes the order in which operations are performed in an arithmetic expression.
Example
Is the result of the preceding example the same as 150 * 3, or as 100 + 150?
Which comes first, addition or multiplication?
As in traditional school math, multiplication is performed first.
Priority is given to multiplication (*) and division (/) over addition (+) and subtraction (-). (-).
In addition, the precedence can be altered using parentheses, just as in elementary school mathematics.
Example
When using parentheses, the operations contained within them are evaluated first.
When multiple operations share the same precedence (such as addition and subtraction), they are evaluated from left to right.
Example
JavaScript Operator Precedence Values
Value | Operator | Description | Example |
---|---|---|---|
21 | ( ) | Expression grouping | (3 + 4) |
20 | . | Member | person.name |
20 | [] | Member | person["name"] |
20 | () | Function call | myFunction() |
20 | new | Create | new Date() |
18 | ++ | Postfix Increment | i++ |
18 | -- | Postfix Decrement | i-- |
17 | ++ | Prefix Increment | ++i |
17 | -- | Prefix Decrement | --i |
17 | ! | Logical not | !(x==y) |
17 | typeof | Type | typeof x |
16 | ** | Exponentiation (ES2016) | 10 ** 2 |
15 | * | Multiplication | 10 * 5 |
15 | / | Division | 10 / 5 |
15 | % | Division Remainder | 10 % 5 |
14 | + | Addition | 10 + 5 |
14 | - | Subtraction | 10 - 5 |
13 | << | Shift left | x << 2 |
13 | >> | Shift right | x >> 2 |
13 | >>> | Shift right (unsigned) | x >>> 2 |
12 | < | Less than | x < y |
12 | <= | Less than or equal | x <= y |
12 | > | Greater than | x > y |
12 | >= | Greater than or equal | x >= y |
12 | in | Property in Object | "PI" in Math |
12 | instanceof | Instance of Object | instanceof Array |
11 | == | Equal | x == y |
11 | === | Strict equal | x === y |
11 | != | Unequal | x != y |
11 | !== | Strict unequal | x !== y |
10 | & | Bitwise AND | x & y |
9 | ^ | Bitwise XOR | x ^ y |
8 | | | Bitwise OR | x | y |
7 | && | Logical AND | x && y |
6 | || | Logical OR | x || y |
5 | ?? | Nullish Coalescing | x ?? y |
4 | ? : | Condition | ? "Yes" : "No" |
3 | += | Assignment | x += y |
3 | /= | Assignment | x /= y |
3 | -= | Assignment | x -= y |
3 | *= | Assignment | x *= y |
3 | %= | Assignment | x %= y |
3 | <<= | Assignment | x <<= y |
3 | >>= | Assignment | x >>= y |
3 | >>>= | Assignment | x >>>= y |
3 | &= | Assignment | x &= y |
3 | ^= | Assignment | x ^= y |
3 | |= | Assignment | x |= y |
2 | yield | Pause Function | yield x |
1 | , | Comma | 5 , 6 |
Expressions in parentheses are fully computed before the value is used in the rest of the expression.
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP