6 Operators
6 Operators
Operators
JavaScript language supports following type of operators.
01 02 03 04
Arithmetic
Operators
Arithmetic Operators
% Modulus Operator and remainder of after an integer division B%A will give 0
Given that x=5, the table below explains the comparison operator:
:
=== 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
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:
In JS, the typeof operator returns the data type of its operand in the form of a string.
Syntax Examples
Examples
console.log(typeof (3.4));
// output: "number"
var myCar;
console.log(typeof myCar);
// output: "undefined "
Like other languages , JS, also allows a programmer to convert a variable/value from
one type to another.
Syntax Of String( )
Syntax Of toString( )
value.toString( )
Examples Of String Conversion
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
Number("") Number(null)
//0 //0
Number("12.2") Number(undefined)
//12.2 //NaN
Examples Of Number Conversion
parseInt("12.2") parseInt(null)
//12 //NaN
parseInt(undefined)
//NaN
Examples Of Number Conversion
parseFloat("") parseFloat(false)
//NaN //NaN
parseFloat("12.2") parseFloat(null)
//12.2 //NaN
parseFloat(undefined)
//NaN
Examples 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:
Boolean(10) Boolean("")
//true //false
Boolean("bhopal") Boolean(null)
//true //false
Boolean(false) Boolean(undefined)
//false //false
Type Coercion
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
true+true+""
"42"+0 //”2"
//"420"
Type Coercion
All other arithmetic operators will try to convert their operands to number and then apply the
operation .
"42" * 7 true%true
//294 //0
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( )
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