Showing posts sorted by relevance for query javascript operators. Sort by date Show all posts
Showing posts sorted by relevance for query javascript operators. Sort by date Show all posts

JavaScript: Operators

Operators compute the values given with and produce result. 

There are different types of JavaScript Operators. These are:

1. Arithmetic Operators

2. Assignment Operators

3. Comparison Operators

4. String Operators

5. Logical Operators

6. Bitwise Operators

7. Ternary Operators and 

8. Type operators

1. Arithmetic Operators:

Arithmetic Operators are used to do arithmetic operations on numbers.

Following arithmetic operators are mostly used in Javascript.

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

**

Exponentiation

/

Division

%

Modulus (Division Remainder)

++

Increment

--

Decrement


Try an example: Try it yourself editor
 
Code
Output

2. Assignment Operators:

Assignment operators assign values to variables. Following are the main assignment operators used in Javascript.

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


Try an example Try it yourself editor
 
Code
Output

3. Comparison Operators:

Comparison operators are used to compare two values. Following comparison values are used to compare the values.

Operator

Description

==

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


All the comparison operators above can also be used on strings, then these are called String Operators.


Try an example Try it yourself editor
 
Code
Output

4. Logical Operators:

Logical Operators corelate two values via 'and', 'or' or 'not'.

Operator

Description

&&

logical and

||

logical or

!

logical not


Try an example Try it yourself editor
 
Code
Output

5. Bitwise Operators:

Bitwise operators work on bit numbers. Any numeric operand in the operation is converted into 32 bit number. The result is converted back to the JavaScript numbers.

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

<< 

left shift

5 << 1

0101 << 1

1010

 10

>> 

right shift

5 >> 1

0101 >> 1

0010

  2

>>> 

unsigned right shift

5 >>> 1

0101 >>> 1

0010

  2


To understand the above operators properly, one should know the binary numbers.

Here 4 bits numbers are given, but JavaScript uses 32 bits signed numbers. Because of this, in JavaScript ~5 will return -6 not 10.


Try an example Try it yourself editor
 
Code
Output

6. Ternary Operators:

These operators are also called conditional operators. This is the only JavaScript operator, takes three operands. These are:

A question followed by a question mark (?)

An expression to execute, if the condition is true.

An expression to execute, if the conditioon is false.

This operator is used as an alternative to if...else statement.

Syntax is:

Condition? Expression if true : Expression if false

Condition: whose value is used as condition.
Expression if true: The expression to be executed, if the condition is true.
Expression if false: The expression to be executed, if the condition is false.
Try an example

Try it yourself editor
 
Code
Output

7. Type Operators:

The type operators in JavaScript works mainly in two methods:

i. Typeof and             ii. instanceof

Typeof operator returns the type of a variable.

Instanceof operator returns 'true' if an object matches the criteria of an object type.

Try an example Try it yourself editor
 
Code
Output

The typeof operator converts a variable in another new variable and data type.

It can convert

Strings to numbers

Numbers to strings

Dates to numbers

Number to dates

Booleans to numbers

Numbers to booleans

These conversions will be described in later chapter.


TOP

Python: Functions