0% found this document useful (0 votes)
6 views16 pages

Operators

Uploaded by

sujiithsivakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Operators

Uploaded by

sujiithsivakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

JavaScript Operators

JavaScript Operators

Assignment operators
Comparison operators
Arithmetic operators
Logical operators
String operators
Conditional (ternary) operator
Comma operator
Unary operators
Relational operators
Assignment operators

Name Shorthand operator Meaning

Assignment x=y x=y

Addition assignment x += y x=x+y

Subtraction assignment x -= y x=x-y

Multiplication assignment x *= y x=x*y

Division assignment x /= y x=x/y

Remainder assignment x %= y x=x%y

Exponentiation assignment x **= y x = x ** y


Comparison operators
Operator Description
Equal (==) Returns true if the operands are equal.

Not equal (!=) Returns true if the operands are not equal.

Returns true if the operands are equal and


Strict equal (===)
of the same type.

Returns true if the operands are of the same


Strict not equal (!==)
type but not equal, or are of different type.

Returns true if the left operand is greater


Greater than (>)
than the right operand.

Returns true if the left operand is greater


Greater than or equal (>=)
than or equal to the right operand.

Returns true if the left operand is less than


Less than (<)
the right operand.

Returns true if the left operand is less than


Less than or equal (<=)
or equal to the right operand.
Arithmetic operators
Operator Description Example

Binary operator. Returns the integer


Remainder (%) remainder of dividing the two 12 % 5 returns 2.
operands.

Unary operator. Adds one to its


operand. If used as a prefix operator
If x is 3, then ++x sets x to 4 and
(++x), returns the value of its operand
Increment (++) returns 4, whereas x++ returns 3 and,
after adding one; if used as a postfix
only then, sets x to 4.
operator (x++), returns the value of its
operand before adding one.

Unary operator. Subtracts one from its


If x is 3, then --x sets x to 2 and
operand. The return value is
Decrement (--) returns 2, whereas x-- returns 3 and,
analogous to that for the increment
only then, sets x to 2.
operator.

Unary operator. Returns the negation


Unary negation (-) If x is 3, then -x returns -3.
of its operand.

Unary operator. Attempts to convert


+"3" returns 3.
Unary plus (+) the operand to a number, if it is not
+true returns 1.
already.

Calculates the base to


2 ** 3 returns 8.
Exponentiation operator (**) the exponent power, that
10 ** -1 returns 0.1.
is, base^exponent
Logical operators

Operator Usage Description

Returns expr1 if it can be


converted to false; otherwise,
returns expr2. Thus, when used
Logical AND (&&) expr1 && expr2 with Boolean
values, && returns true if both
operands are true; otherwise,
returns false.

Returns expr1 if it can be


converted to true; otherwise,
returns expr2. Thus, when used
Logical OR (||) expr1 || expr2 with Boolean
values, || returns true if either
operand is true; if both are
false, returns false.

Returns false if its single


Logical NOT (!) !expr operand that can be converted
to true; otherwise, returns true.
String operators

In addition to the comparison operators, which can


be used on string values, the concatenation operator
(+) concatenates two string values together,
returning another string that is the union of the two
operand strings.
console.log('my ' + 'string'); // console logs the string
"my string".

var mystring = 'alpha'; mystring += 'bet'; // evaluates


to "alphabet" and assigns this value to mystring.
Conditional (ternary) operator

 The conditional operator is the only JavaScript operator that


takes three operands. The operator can have one of two values
based on a condition. The syntax is:

condition ? val1 : val2


 If condition is true, the operator has the value of val1.
Otherwise it has the value of val2. You can use the conditional
operator anywhere you would use a standard operator.

For example,

var status = (age >= 18) ? 'adult' : 'minor';


Comma operator

The comma operator (,) evaluates both of its


operands and returns the value of the last operand.
This operator is primarily used inside a for loop, to
allow multiple variables to be updated each time
through the loop.
For example, if a is a 2-dimensional array with 10
elements on a side, the following code uses the
comma operator to update two variables at once. The
code prints the values of the diagonal elements in the
array:
Comma operator(contd.)

var x = [0,1,2,3,4,5,6,7,8,9]
var a = [x, x, x, x, x];

for (var i = 0, j = 9; i <= j; i++, j--)


// ^
console.log('a[' + i + '][' + j + ']= ' + a[i][j]);
Unary operators

 A unary operation is an operation with only one operand.


 delete
The delete operator deletes an object's property. The syntax
is:

delete object.property;

where object is the name of an object, property is an existing


property, and propertyKey is a string or symbol referring to an
existing property. If the delete operator succeeds, it removes the
property from the object. Trying to access it afterwards will
yield undefined.
Unary operators

typeof
The typeof operator is used in either of the following
ways:
typeof operand
typeof (operand)

The typeof operator returns a string indicating the type


of the unevaluated operand. operand is the string,
variable, keyword, or object for which the type is to be
returned.
Relational operators

A relational operator compares its operands and returns a


Boolean value based on whether the comparison is true.
in
The in operator returns true if the specified property is in
the specified object. The syntax is:

propNameOrNumber in objectName

where propNameOrNumber is a string, numeric, or symbol


expression representing a property name or array index,
and objectName is the name of an object.
Spread operator

Spread syntax (...) allows an iterable such as an array


expression or string to be expanded in places where
zero or more arguments (for function calls) or
elements (for array literals) are expected, or an
object expression to be expanded in places where
zero or more key-value pairs (for object literals) are
expected.
Spread syntax can be used when all elements from
an object or array need to be included in a list of
some kind.
Rest operator

Rest syntax looks exactly like spread syntax. In a


way, rest syntax is the opposite of spread syntax.
Spread syntax "expands" an array into its elements,
while rest syntax collects multiple elements and
"condenses" them into a single element.
Destructuring

 For more complex assignments, the destructuring assignment syntax


is a JavaScript expression that makes it possible to extract data from
arrays or objects using a syntax that mirrors the construction of
array and object literals.
 var foo = ['one', 'two', 'three'];

// without destructuring
var one = foo[0];
var two = foo[1];
var three = foo[2];

// with destructuring
var [one, two, three] = foo;

You might also like