Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

OPERATORS IN JAVASCRIPT

Operators
JavaScript language supports following type of operators.

01 02 03 04

Arithmetic

Comparision Operators Logical Operators Assignment Operators

Operators
Arithmetic Operators

Assume variable A holds 10 and variable B holds 20 then :

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10


* Multiply both operand A * B will give 200

/ Divide numerator by denominator B/A will give 2

% Modulus Operator and remainder of after an integer division B%A will give 0

++ Increment operator, increases integer value by one A++ will give 11

-- Decrement operator, decreases integer value by one A-- will give 9


Comparison Operators

Given that x=5, the table below explains the comparison operator:
:

Operator Description Example

== Is equal to x==8 false x==5 true

=== Is exactly equal to(value and type) x===“5” false x===5 true
!= Is not equal x != 8 true

!== Is not equal (either value or type) x !==“5” true x!==5 false

> Is greater than x>8 false


< Is less than x<8 true

>= Is greater than or equal to x>=8 false


<= Is less than or equal to x<=8 true
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:

Operator Description Example

&& AND (x < 10 && y > 1) is true

|| OR (x==5 || y==5) is false

! NOT !(x==y) is true


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:

Operator Example Same As Result


= x=y x=5

+= x+=y x=x+y x=15


-= x-=y x=x-y x=5
*= x*=y x=x*y x=50
/= x/=y x=x/y x=2
%= x%=y x=x%y x=0
The typeof Operator

In JS, the typeof operator returns the data type of its operand in the form of a string.

Operand can be variables , expressions and even objects.

Syntax Examples

typeof operand console.log(typeof 42);


OR // output: "number“
typeof (operand) console.log(typeof ‘bhopal');
// output: "string“
console.log(typeof true);
// output: "boolean“
console.log(typeof (3+4));
// output: “number"
Some More Examples

Examples

console.log(typeof (3.4));
// output: "number"

var myCar;
console.log(typeof myCar);
// output: "undefined "

var myBike= " ";


console.log(typeof myBike); This is considered to
// output: "string“ be a bug within JS as
it is expected that null
var myBicycle=null; itself is a data type.
More on this:https://2ality.com/
console.log(typeof myBicycle); 2013/10/typeof-null.html
// output: "object"
Type Conversion & Type Coercion

Like other languages , JS, also allows a programmer to convert a variable/value from
one type to another.

This is called Type Conversion.

On the other hand if this conversion is done by JS itself , then we call it as


Type Coercion
Converting Values To String
To convert a value to a string , JS provides 2 ways:

1. Using the global function called String( )


2. Using the method toString( ) on the value to be converted

Syntax Of String( )

String (value to be converted)

Syntax Of toString( )
value.toString( )
Examples Of String Conversion

Basic Examples Some Special Examples

String(10) String(null) Remember , that the


//"10" //"null" method toString()
doesn’t work with null
(10).toString() String(undefined) and undefined types .
//"10" //"undefined“ Although it works with
NaN
String(true) String(NaN)
//"true" //"NaN”

true.toString() NaN.toString()
//"true" //"NaN”

String(false)
//"false"

false.toString()
//"false”
Converting Values To Number
To convert a value to a number , JS provides 3 ways:
Remember , that
1. Using the global function called Number( ) parseInt(),
2. Using the function parseInt( ) or parseFloat( ) parseFloat() can
only
3. Using the method Math.floor( ) be used to convert
String values to
Syntax Of Number( ) number and not for
Number (value to be converted) boolean values

Syntax Of parseInt( )
parseInt (value to be converted)

Syntax Of parseFloat( )
parseFloat (value to be converted)

Syntax Of Math.floor( )
Math.floor(value to be converted)
Examples Of Number Conversion

Basic Examples Some Special Examples


Number("1") Number("1A2")
//1 //NaN If a string contains
Remember , strings invalid characters,
Number("0") are trimmed before Number(true) the function
//0 being converted to //1 Number( ) will
number generate a NaN.
Number(" 1 ") Number(false)
//1 //0

Number("") Number(null)
//0 //0

Number("12.2") Number(undefined)
//12.2 //NaN
Examples Of Number Conversion

Basic Examples Some Special Examples


parseInt("1") parseInt("1A2")
//1 If a string contains
//1
invalid characters,
Remember , that
parseInt("0") these functions
the functions parseInt("A12")
//0 extract initial digits
parseInt() and //NaN
and return it as a
parseFloat() return
parseInt(" 1 ") number. If no initial
NaN for empty strings parseInt(true)
//1 digits are present or
//NaN
value is boolean they
parseInt("") return NaN
parseInt(false)
//NaN //NaN

parseInt("12.2") parseInt(null)
//12 //NaN

parseInt(undefined)
//NaN
Examples Of Number Conversion

Basic Examples Some Special Examples


parseFloat("1") parseFloat("1A2")
//1 //1

parseFloat ("0") parseFloat("A12")


//0 //NaN

parseFloat(" 1 ") parseFloat(true)


//1 //NaN

parseFloat("") parseFloat(false)
//NaN //NaN

parseFloat("12.2") parseFloat(null)
//12.2 //NaN

parseFloat(undefined)
//NaN
Examples Of Number Conversion

Basic Examples Some Special Examples


Math.floor("1") Math.floor("1A2") If a string contains
//1 //NaN invalid characters,
the mehod
Remember , that
Math.floor("0") Math.floor() will
like Number( )
//0 Math.floor(true) generate a NaN.
function, the method
Math.floor() also //1
Math.floor(" 1 ") returns 0 for
//1 empty string Math.floor(false)
//0
Math.floor("")
//0 Math.floor(null)
//0
Math.floor("12.2")
//12 Math.floor(undefined)
//NaN
Standard Rules Of Number Conversion

Value Becomes…
undefined NaN
null 0
true and false 1 and 0
Whitespaces from the start and end are
removed. If the remaining string is
string empty, the result is 0. Otherwise, the
number is “read” from the string. An
error gives NaN.
Converting Values To Boolean
To convert a value to a number , JS provides only 1 way:

1. Using the global function called Boolean( ) IMPORTANT POINTS:

1. Any value can be converted


to boolean passing it
to Boolean().
Syntax Of Boolean( )
Boolean(value to be converted) 2. All values will resolve
to true except
FALSY VALUES, which are
0,””,NaN,null and undefined
Examples Of Boolean Conversion

Basic Examples Some Special Examples

Boolean(10) Boolean("")
//true //false

Boolean(0) Boolean(" ")


//false //true

Boolean("bhopal") Boolean(null)
//true //false

Boolean(false) Boolean(undefined)
//false //false
Type Coercion

As discussed previously , there are some cases in JS , when it automatically converts


value of one type to another and this is called Type Coercion

The Behaviour Of Operator “+”:

If any of the operands is a string , all others are converted to string and concatenation is
done ,otherwise arithmetic addition is done.
Examples Of Type Coercion

Basic Examples Some Special Examples


10+20 true+false
//30 //1

" Good " + " Morning " true+true


// "GoodMorning" //2

true+true+""
"42"+0 //”2"
//"420"
Type Coercion

The Behaviour Of Other Arithmetic Operators:

All other arithmetic operators will try to convert their operands to number and then apply the
operation .

If conversion fails they give NaN.


Examples Of Type Coercion

Basic Examples Some Special Examples


10*20 true * false
//200 //0

" Good " * " Morning " true/false


// NaN //Infinity

"42" * 7 true%true
//294 //0

"42" - 7 "Bhopal" /10


//35 //NaN
Controlling Decimal Points
Normally JS, displays numeric values upto 16 decimal places while displaying floats ,
leaving trailing zeros.

To understand this behavior consider the following codes and their output

Example

let x=10/3;
console.log(x)
//3.3333333333333335

x=10/4;
console.log(x)
//2.5

x=3.0;
console.log(x)
//3
Controlling Decimal Points
To control this behavior , JS provides us a method called toFixed() for number type
values , which can be used to write a number with specified number of decimals

Syntax Of toFixed( )

numericValue.toFixed (some number)


Controlling Decimal Points

Example

let x=10/3;
console.log(x.toFixed(2))
//3.33

x=10/4;
console.log(x.toFixed(3))
//2.500

x=3.0;
console.log(x.toFixed(1))
//3.0

x=10/3;
console.log(x.toFixed(0))
//3

You might also like