SlideShare a Scribd company logo
www.webstackacademy.com ‹#›
Operators
JavaScript
www.webstackacademy.com ‹#›

Operators
Table of Content
www.webstackacademy.com ‹#›www.webstackacademy.com ‹#›
Operators
(JavaScript)
www.webstackacademy.com ‹#›
JS - Operators
●
Symbols that instructs the compiler to perform specific
arithmetic or logical operation on operands
●
All operators prominently do two things
– Operate on its operands
– Return a result
www.webstackacademy.com ‹#›
JS - Operators
Category
Operand
Operation
unary
ternary
Arithmetic
Logical
binary
+
!
Relational >
Assignment =
Bitwise &
Language typeof
Member access .
- * / %
|| &&
< <= >= == !=
| ^ ~ >> <<
...
...
...
...
www.webstackacademy.com ‹#›
JS - Operators
●
JavaScript Supports following operators:
– Arithmetic Operators
– Assignment Operators
– Comparison Operators
– Ternary/Conditional Operator
– Logical Operators
– Type Operators
– Bitwise Operators
www.webstackacademy.com ‹#›
JS – Operators Table
Operator Associativity
( ) NA
. [ ] left-to-right
new ( w/ argument list ) NA
function call
new ( w/o argument list )
left-to-right
right-to-left
exp++ exp-- right-to-left
! ~ ++exp --exp + - void typeof delete right-to-left
* / % left-to-right
+ - left-to-right
<< >> >>> left-to-right
High
Low
www.webstackacademy.com ‹#›
JS – Operators Table
Operator Associativity
< <= > >= in instanceof left-to-right
== != === !== left-to-right
& left-to-right
^ left-to-right
| left-to-right
&& left-to-right
|| left-to-right
? : right-to-left
yield right-to-left
= += -= *= /= %= <<= >>= >>>== &= |= ^= right-to-left
, left-to-right
High
Low
www.webstackacademy.com ‹#›
Arithmetic operator
Name Description
+ Returns arithmetic addition
- Return arithmetic subtraction
* Returns arithmetic multiplication
/ Returns arithmetic division
% Returns modulus (remainder)
www.webstackacademy.com ‹#›
Unary operators
Name Description
+ Converts its operand to Number type
- Converts its operand to Number type and then negates it
++exp
Increment the value by one and store back in variable. Returns new
incremented value
--exp
Decrement the value by one and store back in variable. Returns new
decremented value
exp++ Returns the old value. Increment the value by one and store back in variable.
exp-- Returns the old value. Decrement the value by one and store back in
variable.
www.webstackacademy.com ‹#›
Assignment operator
Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
<<= a <<= b a = a << b
>>= a >>= b a = a >> b
>>>= a >>>= b a = a >>> b
&= a &= b a = a & b
^= a ^= b a = a ^ b
|= a |= b a = a |= b
www.webstackacademy.com ‹#›
Comparison operator
Operator Description
== Equal to (compare values)
=== Equal value and equal type
!= Not equal to
!== Not equal value or not equal type
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
www.webstackacademy.com ‹#›
Comparison operator
●
When comparing a string with a number, JavaScript will
convert the string to a number when doing the comparison
●
An empty string converts to 0
●
A non-numeric string converts to NaN which is always false
●
Two strings are compared in alphabetical order
●
Objects can’t be compared
www.webstackacademy.com ‹#›
Ternary/Conditional
Syntax:
Condition ? expr1 : expr2
Example:
status = (marks >= 50) ? "Pass" : "Fail";
●
Used as a shortcut for standard “if” statement
●
It takes three operands
www.webstackacademy.com ‹#›
Class Work - Ternary
●
WAP to find the max of given two numbers
●
WAP to return the absolute value of a number
●
WAP to check if given number is even or odd
www.webstackacademy.com ‹#›
Logical operators
Name Description
&& Logical AND
|| Logical OR
! Logical NOT
<script>
var num1 = 1, num2 = 0;
if (++num1 || num2++) {
document.write("Wow! its true!!");
}
else {
document.write("Hmm! its false");
}
document.write("<br> num1 = " + num1);
document.write("<br> num2 = " + num2);
</script>
www.webstackacademy.com ‹#›
Logical operators
●
Logical expressions are evaluated left to right
●
They are tested for possible "short-circuit" evaluation using
the following rules
– false && (don’t care exp) is short-circuit evaluated to
false
– true || (don’t care exp) is short-circuit evaluated to true
●
Please note that “don’t care exp” is not evaluated
www.webstackacademy.com ‹#›
Boolean conversion to false
The Boolean value of 0 (zero) is false Boolean(0)
The Boolean value of -0 (minus zero) is false Boolean(-0)
The Boolean value of "" (empty string) is false Boolean(“”)
The Boolean value of undefined is false Boolean(undefined)
The Boolean value of null is false Boolean(null)
The Boolean value of NaN is false Boolean(NaN)
www.webstackacademy.com ‹#›
The typeof operator
Syntax:
typeof operand or typeof (operand)
●
The typeof operator is used to get the data type of its
operand
●
The operand can be either a literal or a data structure
such as a variable, a function, or an object
●
The operator returns the data type
www.webstackacademy.com ‹#›
The typeof operator
●
There are six possible values that typeof returns: object,
boolean, function, number, string and undefined
Example :
typeof “abc” // returns string
typeof Nan // returns number
typeof false //returns boolean
typeof [3,4,5,6] //returns Object
www.webstackacademy.com ‹#›
The instanceof operator
●
The instanceof operator returns true if the specified
object is an instance of the specified object
Syntax :
var result = <objectName> instanceof <objectType>;
www.webstackacademy.com ‹#›
The delete operator
●
The delete operator removes an object's property
completely
●
Delete operator removes an element from array; array
length does not get affected
●
The operator returns true on successful deletion, else
false will be returned
www.webstackacademy.com ‹#›
The delete operator
Syntax :
delete objectName.property;
delete objectName[‘property’];
delete arrayName[index];
Example :
delete arr[0];
www.webstackacademy.com ‹#›
The delete operator
●
Delete operator has nothing to do with directly freeing
memory (does not free memory)
●
If the property which is being deleted does not exist,
delete will not have any effect and will return true
●
If a property with the same name exists on the object's
prototype chain, then, after deletion, the object will use
the property from the prototype chain (in other words,
delete only has an effect on own properties)
www.webstackacademy.com ‹#›
The delete operator
●
Non-configurable properties cannot be removed
– This includes properties of built-in objects like Math,
Array, Object and properties that are created as non-
configurable with methods like Object.defineProperty()
– var, let and const create non-configurable properties
that cannot be deleted
www.webstackacademy.com ‹#›
The new operator
●
The new operator is used to create an instance of a user-
defined object type or one of built in object types which
have a constructor function
Syntax:
var objectName = new objectType(param1, param2, ...., paramN);
www.webstackacademy.com ‹#›
The comma operator
●
The comma operator (,) is used to execute two
expressions sequentially
●
The value of right operand is used as result of comma
operator
Syntax :
left-operand, right-operand;
www.webstackacademy.com ‹#›
The this operator
●
The this operator is used to refer to current object
Syntax:
this.propertyName
www.webstackacademy.com ‹#›
The void operator
●
The void operator is used to evaluate a JavaScript
expression without returning a value
Syntax :
void (expression)
void expression
www.webstackacademy.com ‹#›
The void operator
●
This operator allows evaluating expressions that produce a value
into places where an expression that evaluates to undefined is
desired
●
The void operator is often used merely to obtain the undefined
primitive value, usually using "void(0)"
Example :
num1 = void(0);
www.webstackacademy.com ‹#›
Bitwise operator
●
All numbers are stored as 64 bits floating point in JavaScript
●
All bitwise operations are performed on 32 bits binary numbers
●
Therefore, 64 bits floating point numbers are converted to 32
bits integers by JavaScript before performing bitwise operation
●
The result of bitwise operation is a signed 32-bit integer
●
The result of bitwise operation is converted back to 64 bits
JavaScript number
www.webstackacademy.com ‹#›
Primitive data type
(Number)
Position
Bit No of Bits
Value
31
0
30
0
29
0
28
0
27
0
26
0
25
0
24
0
23
0
22
0
21
0
20
0
19
0
18
0
17
0
16
0
15
0
14
0
13
0
12
0
11
0
10
0
9
0
8
0
7
0
6
0
5
0
4
0
3
1
2
1
1
0
0
1
●
Integers are like whole numbers, but allow negative
numbers and no fraction
● An example of 1310 in 32 bit system would be
www.webstackacademy.com ‹#›
●
Negative Integers represented with the 2's complement of the
positive number
● An example of -1310 in 32 bit system would be
Position
Bit No of Bits
Value
31
0
30
0
29
0
28
0
27
0
26
0
25
0
24
0
23
0
22
0
21
0
20
0
19
0
18
0
17
0
16
0
15
0
14
0
13
0
12
0
11
0
10
0
9
0
8
0
7
0
6
0
5
0
4
0
3
1
2
1
1
0
0
1
1's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
Add 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
2's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
●
Mathematically : -k ≡ 2
n
- k
Primitive data type
(Number)
www.webstackacademy.com ‹#›
Bitwise operator
Operator Name Description
& Bitwise AND Performs bitwise AND operation
| Bitwise OR Performs bitwise OR operation
~ Bitwise NOT Also known as complement; It flips the bits
^ Bitwise XOR Performs bitwise XOR operation
<< Left shift (zero-fill) Performs bitwise left shift zero filling operation
>> Right shift (sign-fill) Performs bitwise right shift sign filling operation
>>> Right shift (zero-fill) Performs bitwise right shift zero filling operation
www.webstackacademy.com ‹#›
Bitwise operators
0x61
Value
0x13
0
0
1
0
1
0
0
1
0
0
0
0
0
1
1
1
00x60
Value
0x13
A
Operand
B
0x01 0 0 0 0 0 0 0 10x13A & B
& Bitwise AND
Bitwise ANDing of
all the bits in two
operands
0x61
Value
0x13
0
0
1
0
1
0
0
1
0
0
0
0
0
1
1
1
00x60
Value
0x13
A
Operand
B
0x73 0 1 1 1 0 0 1 10x13A | B
| Bitwise OR
Bitwise ORing of
all the bits in two
operands
www.webstackacademy.com ‹#›
0x61
Value
0x13
0
0
1
0
1
0
0
1
0
0
0
0
0
1
1
1
00x60
Value
0x13
A
Operand
B
0x72 0 1 1 1 0 0 1 00x13A ^ B
^ Bitwise XOR
Bitwise XORing of
all the bits in two
operands
0x61
Value
0 1 1 0 0 0 0 100x60
Value
A
Operand
0x9E 1 0 0 1 1 1 1 00x13~A
~ Complement
Complementing
all the bits of the
operand
Bitwise operators
www.webstackacademy.com ‹#›
Left Shift :
shift-expression << additive-expression
(left operand) (right operand)
Right Shift :
shift-expression >> additive-expression
(left operand) (right operand)
shift-expression >>> additive-expression
(left operand) (right operand)
Bitwise operators
www.webstackacademy.com ‹#›
'Value' << 'Bits Count'
●
Value : Is shift operand on which bit shifting effect to be applied
●
Bits count : By how many bit(s) the given “Value” to be shifted
0x61Original value
0x84
A << 2
Resultant value
0000110 1
0100001 0
Say A = 0x61
Zero filling left shift
Bitwise operators
www.webstackacademy.com ‹#›
'Value' >> 'Bits Count'
●
Value : Is shift operand on which bit shifting effect to be applied
●
Bits count : By how many bit(s) the given “Value” to be shifted
Sign-bit filling right shift
0x61Original value
0x18
A >> 2
Resultant value
0000110 1
0011000 0
Say A = 0x61
Bitwise operators
www.webstackacademy.com ‹#›
Sign-bit filling right shift
0xA1Original value
0xE8
A >> 2
Resultant value
0000101 1
0010111 0
Say A = -95
Bitwise operators
www.webstackacademy.com ‹#›
Bitwise operator
(8-bit system examples)
Operator Example Same as Result Same as
& 5 & 1 0000 0101 & 0000 0001 1 0000 0001
| 5 | 2 0000 0101 | 0000 0010 7 0000 0111
~ ~ 5 ~0000 0101 -6 1111 1010
^ 5 ^ 1 0000 0101 ^ 0000 0001 4 0000 0100
<< 5 << 2 0000 0101 << 2 20 0001 0100
>> -5 >> 2 1111 1011 >> 2 -2 1111 1110
>>> -5 >>> 2 1111 1011 >>> 2 62 0011 1110
www.webstackacademy.com ‹#›
Class Work
●
WAP to print bits of a given number
●
WAP to count set bits in a given number
www.webstackacademy.com ‹#›
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com

More Related Content

PDF
JavaScript - Chapter 10 - Strings and Arrays
PPTX
Javascript event handler
PDF
JavaScript - Chapter 6 - Basic Functions
PDF
JavaScript - Chapter 8 - Objects
PDF
JavaScript - Chapter 15 - Debugging Techniques
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
PDF
JavaScript - Chapter 12 - Document Object Model
PPT
Introduction to JavaScript
JavaScript - Chapter 10 - Strings and Arrays
Javascript event handler
JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 8 - Objects
JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 12 - Document Object Model
Introduction to JavaScript

What's hot (20)

PPTX
Javascript functions
PPT
Introduction to Javascript
PPTX
Event In JavaScript
PPSX
Javascript variables and datatypes
PDF
3. Java Script
PDF
JavaScript - Chapter 11 - Events
PDF
Javascript basics
PDF
TypeScript Introduction
PPTX
Lab #2: Introduction to Javascript
PDF
Angular - Chapter 4 - Data and Event Handling
PDF
Basics of JavaScript
PPT
Js ppt
PPT
JavaScript & Dom Manipulation
PDF
Asp.net state management
PPT
Switch statements in Java
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
PPTX
Form Handling using PHP
PPT
PHP - Introduction to Object Oriented Programming with PHP
PDF
Arrays in Java
Javascript functions
Introduction to Javascript
Event In JavaScript
Javascript variables and datatypes
3. Java Script
JavaScript - Chapter 11 - Events
Javascript basics
TypeScript Introduction
Lab #2: Introduction to Javascript
Angular - Chapter 4 - Data and Event Handling
Basics of JavaScript
Js ppt
JavaScript & Dom Manipulation
Asp.net state management
Switch statements in Java
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
Form Handling using PHP
PHP - Introduction to Object Oriented Programming with PHP
Arrays in Java
Ad

Similar to JavaScript - Chapter 5 - Operators (20)

PDF
Chapter 01 Introduction to Java by Tushar B Kute
PPTX
C sharp part 001
PPTX
JAVASCRIPT - LinkedIn
PDF
Introduction to JavaScript for APEX Developers - Module 1: JavaScript Basics
PPTX
Java script basics
PDF
PPT
Introduction to javascript.ppt
PPTX
Operators In Java Part - 8
PPTX
Introduction to java script
PDF
Fii Practic Frontend - BeeNear - laborator3
PPTX
Chapter 3:Programming with Java Operators and Strings
PPTX
Pi j1.3 operators
PDF
JavaScript - Chapter 4 - Types and Statements
DOCX
PPTX
Chapter 3
ODP
Preparing Java 7 Certifications
PDF
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
PPTX
Javascript omg!
PDF
JS Responsibilities
PPTX
Operators in java
Chapter 01 Introduction to Java by Tushar B Kute
C sharp part 001
JAVASCRIPT - LinkedIn
Introduction to JavaScript for APEX Developers - Module 1: JavaScript Basics
Java script basics
Introduction to javascript.ppt
Operators In Java Part - 8
Introduction to java script
Fii Practic Frontend - BeeNear - laborator3
Chapter 3:Programming with Java Operators and Strings
Pi j1.3 operators
JavaScript - Chapter 4 - Types and Statements
Chapter 3
Preparing Java 7 Certifications
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Javascript omg!
JS Responsibilities
Operators in java
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
PDF
Career Building in AI - Technologies, Trends and Opportunities
PDF
Webstack Academy - Internship Kick Off
PDF
Building Your Online Portfolio
PDF
Front-End Developer's Career Roadmap
PDF
Angular - Chapter 9 - Authentication and Authorization
PDF
Angular - Chapter 7 - HTTP Services
PDF
Angular - Chapter 6 - Firebase Integration
PDF
Angular - Chapter 5 - Directives
PDF
Angular - Chapter 3 - Components
PDF
Angular - Chapter 2 - TypeScript Programming
PDF
Angular - Chapter 1 - Introduction
PDF
JavaScript - Chapter 14 - Form Handling
PDF
JavaScript - Chapter 7 - Advanced Functions
PDF
JavaScript - Chapter 3 - Introduction
PDF
JavaScript - Chapter 1 - Problem Solving
PDF
jQuery - Chapter 4 - DOM Handling
PDF
jQuery - Chapter 5 - Ajax
Webstack Academy - Course Demo Webinar and Placement Journey
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Course Demo Webinar - Full Stack Developer Course
Career Building in AI - Technologies, Trends and Opportunities
Webstack Academy - Internship Kick Off
Building Your Online Portfolio
Front-End Developer's Career Roadmap
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 7 - HTTP Services
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 5 - Directives
Angular - Chapter 3 - Components
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 1 - Introduction
JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 1 - Problem Solving
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 5 - Ajax

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
A Presentation on Artificial Intelligence
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Machine Learning_overview_presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
A Presentation on Artificial Intelligence
“AI and Expert System Decision Support & Business Intelligence Systems”
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine Learning_overview_presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

JavaScript - Chapter 5 - Operators

  • 4. www.webstackacademy.com ‹#› JS - Operators ● Symbols that instructs the compiler to perform specific arithmetic or logical operation on operands ● All operators prominently do two things – Operate on its operands – Return a result
  • 5. www.webstackacademy.com ‹#› JS - Operators Category Operand Operation unary ternary Arithmetic Logical binary + ! Relational > Assignment = Bitwise & Language typeof Member access . - * / % || && < <= >= == != | ^ ~ >> << ... ... ... ...
  • 6. www.webstackacademy.com ‹#› JS - Operators ● JavaScript Supports following operators: – Arithmetic Operators – Assignment Operators – Comparison Operators – Ternary/Conditional Operator – Logical Operators – Type Operators – Bitwise Operators
  • 7. www.webstackacademy.com ‹#› JS – Operators Table Operator Associativity ( ) NA . [ ] left-to-right new ( w/ argument list ) NA function call new ( w/o argument list ) left-to-right right-to-left exp++ exp-- right-to-left ! ~ ++exp --exp + - void typeof delete right-to-left * / % left-to-right + - left-to-right << >> >>> left-to-right High Low
  • 8. www.webstackacademy.com ‹#› JS – Operators Table Operator Associativity < <= > >= in instanceof left-to-right == != === !== left-to-right & left-to-right ^ left-to-right | left-to-right && left-to-right || left-to-right ? : right-to-left yield right-to-left = += -= *= /= %= <<= >>= >>>== &= |= ^= right-to-left , left-to-right High Low
  • 9. www.webstackacademy.com ‹#› Arithmetic operator Name Description + Returns arithmetic addition - Return arithmetic subtraction * Returns arithmetic multiplication / Returns arithmetic division % Returns modulus (remainder)
  • 10. www.webstackacademy.com ‹#› Unary operators Name Description + Converts its operand to Number type - Converts its operand to Number type and then negates it ++exp Increment the value by one and store back in variable. Returns new incremented value --exp Decrement the value by one and store back in variable. Returns new decremented value exp++ Returns the old value. Increment the value by one and store back in variable. exp-- Returns the old value. Decrement the value by one and store back in variable.
  • 11. www.webstackacademy.com ‹#› Assignment operator Operator Example Same as = a = b a = b += a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a = a/b %= a %= b a = a%b <<= a <<= b a = a << b >>= a >>= b a = a >> b >>>= a >>>= b a = a >>> b &= a &= b a = a & b ^= a ^= b a = a ^ b |= a |= b a = a |= b
  • 12. www.webstackacademy.com ‹#› Comparison operator Operator Description == Equal to (compare values) === Equal value and equal type != Not equal to !== Not equal value or not equal type > Greater than < Less than >= Greater than or equal to <= Less than or equal to
  • 13. www.webstackacademy.com ‹#› Comparison operator ● When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison ● An empty string converts to 0 ● A non-numeric string converts to NaN which is always false ● Two strings are compared in alphabetical order ● Objects can’t be compared
  • 14. www.webstackacademy.com ‹#› Ternary/Conditional Syntax: Condition ? expr1 : expr2 Example: status = (marks >= 50) ? "Pass" : "Fail"; ● Used as a shortcut for standard “if” statement ● It takes three operands
  • 15. www.webstackacademy.com ‹#› Class Work - Ternary ● WAP to find the max of given two numbers ● WAP to return the absolute value of a number ● WAP to check if given number is even or odd
  • 16. www.webstackacademy.com ‹#› Logical operators Name Description && Logical AND || Logical OR ! Logical NOT <script> var num1 = 1, num2 = 0; if (++num1 || num2++) { document.write("Wow! its true!!"); } else { document.write("Hmm! its false"); } document.write("<br> num1 = " + num1); document.write("<br> num2 = " + num2); </script>
  • 17. www.webstackacademy.com ‹#› Logical operators ● Logical expressions are evaluated left to right ● They are tested for possible "short-circuit" evaluation using the following rules – false && (don’t care exp) is short-circuit evaluated to false – true || (don’t care exp) is short-circuit evaluated to true ● Please note that “don’t care exp” is not evaluated
  • 18. www.webstackacademy.com ‹#› Boolean conversion to false The Boolean value of 0 (zero) is false Boolean(0) The Boolean value of -0 (minus zero) is false Boolean(-0) The Boolean value of "" (empty string) is false Boolean(“”) The Boolean value of undefined is false Boolean(undefined) The Boolean value of null is false Boolean(null) The Boolean value of NaN is false Boolean(NaN)
  • 19. www.webstackacademy.com ‹#› The typeof operator Syntax: typeof operand or typeof (operand) ● The typeof operator is used to get the data type of its operand ● The operand can be either a literal or a data structure such as a variable, a function, or an object ● The operator returns the data type
  • 20. www.webstackacademy.com ‹#› The typeof operator ● There are six possible values that typeof returns: object, boolean, function, number, string and undefined Example : typeof “abc” // returns string typeof Nan // returns number typeof false //returns boolean typeof [3,4,5,6] //returns Object
  • 21. www.webstackacademy.com ‹#› The instanceof operator ● The instanceof operator returns true if the specified object is an instance of the specified object Syntax : var result = <objectName> instanceof <objectType>;
  • 22. www.webstackacademy.com ‹#› The delete operator ● The delete operator removes an object's property completely ● Delete operator removes an element from array; array length does not get affected ● The operator returns true on successful deletion, else false will be returned
  • 23. www.webstackacademy.com ‹#› The delete operator Syntax : delete objectName.property; delete objectName[‘property’]; delete arrayName[index]; Example : delete arr[0];
  • 24. www.webstackacademy.com ‹#› The delete operator ● Delete operator has nothing to do with directly freeing memory (does not free memory) ● If the property which is being deleted does not exist, delete will not have any effect and will return true ● If a property with the same name exists on the object's prototype chain, then, after deletion, the object will use the property from the prototype chain (in other words, delete only has an effect on own properties)
  • 25. www.webstackacademy.com ‹#› The delete operator ● Non-configurable properties cannot be removed – This includes properties of built-in objects like Math, Array, Object and properties that are created as non- configurable with methods like Object.defineProperty() – var, let and const create non-configurable properties that cannot be deleted
  • 26. www.webstackacademy.com ‹#› The new operator ● The new operator is used to create an instance of a user- defined object type or one of built in object types which have a constructor function Syntax: var objectName = new objectType(param1, param2, ...., paramN);
  • 27. www.webstackacademy.com ‹#› The comma operator ● The comma operator (,) is used to execute two expressions sequentially ● The value of right operand is used as result of comma operator Syntax : left-operand, right-operand;
  • 28. www.webstackacademy.com ‹#› The this operator ● The this operator is used to refer to current object Syntax: this.propertyName
  • 29. www.webstackacademy.com ‹#› The void operator ● The void operator is used to evaluate a JavaScript expression without returning a value Syntax : void (expression) void expression
  • 30. www.webstackacademy.com ‹#› The void operator ● This operator allows evaluating expressions that produce a value into places where an expression that evaluates to undefined is desired ● The void operator is often used merely to obtain the undefined primitive value, usually using "void(0)" Example : num1 = void(0);
  • 31. www.webstackacademy.com ‹#› Bitwise operator ● All numbers are stored as 64 bits floating point in JavaScript ● All bitwise operations are performed on 32 bits binary numbers ● Therefore, 64 bits floating point numbers are converted to 32 bits integers by JavaScript before performing bitwise operation ● The result of bitwise operation is a signed 32-bit integer ● The result of bitwise operation is converted back to 64 bits JavaScript number
  • 32. www.webstackacademy.com ‹#› Primitive data type (Number) Position Bit No of Bits Value 31 0 30 0 29 0 28 0 27 0 26 0 25 0 24 0 23 0 22 0 21 0 20 0 19 0 18 0 17 0 16 0 15 0 14 0 13 0 12 0 11 0 10 0 9 0 8 0 7 0 6 0 5 0 4 0 3 1 2 1 1 0 0 1 ● Integers are like whole numbers, but allow negative numbers and no fraction ● An example of 1310 in 32 bit system would be
  • 33. www.webstackacademy.com ‹#› ● Negative Integers represented with the 2's complement of the positive number ● An example of -1310 in 32 bit system would be Position Bit No of Bits Value 31 0 30 0 29 0 28 0 27 0 26 0 25 0 24 0 23 0 22 0 21 0 20 0 19 0 18 0 17 0 16 0 15 0 14 0 13 0 12 0 11 0 10 0 9 0 8 0 7 0 6 0 5 0 4 0 3 1 2 1 1 0 0 1 1's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 Add 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 ● Mathematically : -k ≡ 2 n - k Primitive data type (Number)
  • 34. www.webstackacademy.com ‹#› Bitwise operator Operator Name Description & Bitwise AND Performs bitwise AND operation | Bitwise OR Performs bitwise OR operation ~ Bitwise NOT Also known as complement; It flips the bits ^ Bitwise XOR Performs bitwise XOR operation << Left shift (zero-fill) Performs bitwise left shift zero filling operation >> Right shift (sign-fill) Performs bitwise right shift sign filling operation >>> Right shift (zero-fill) Performs bitwise right shift zero filling operation
  • 35. www.webstackacademy.com ‹#› Bitwise operators 0x61 Value 0x13 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 00x60 Value 0x13 A Operand B 0x01 0 0 0 0 0 0 0 10x13A & B & Bitwise AND Bitwise ANDing of all the bits in two operands 0x61 Value 0x13 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 00x60 Value 0x13 A Operand B 0x73 0 1 1 1 0 0 1 10x13A | B | Bitwise OR Bitwise ORing of all the bits in two operands
  • 36. www.webstackacademy.com ‹#› 0x61 Value 0x13 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 00x60 Value 0x13 A Operand B 0x72 0 1 1 1 0 0 1 00x13A ^ B ^ Bitwise XOR Bitwise XORing of all the bits in two operands 0x61 Value 0 1 1 0 0 0 0 100x60 Value A Operand 0x9E 1 0 0 1 1 1 1 00x13~A ~ Complement Complementing all the bits of the operand Bitwise operators
  • 37. www.webstackacademy.com ‹#› Left Shift : shift-expression << additive-expression (left operand) (right operand) Right Shift : shift-expression >> additive-expression (left operand) (right operand) shift-expression >>> additive-expression (left operand) (right operand) Bitwise operators
  • 38. www.webstackacademy.com ‹#› 'Value' << 'Bits Count' ● Value : Is shift operand on which bit shifting effect to be applied ● Bits count : By how many bit(s) the given “Value” to be shifted 0x61Original value 0x84 A << 2 Resultant value 0000110 1 0100001 0 Say A = 0x61 Zero filling left shift Bitwise operators
  • 39. www.webstackacademy.com ‹#› 'Value' >> 'Bits Count' ● Value : Is shift operand on which bit shifting effect to be applied ● Bits count : By how many bit(s) the given “Value” to be shifted Sign-bit filling right shift 0x61Original value 0x18 A >> 2 Resultant value 0000110 1 0011000 0 Say A = 0x61 Bitwise operators
  • 40. www.webstackacademy.com ‹#› Sign-bit filling right shift 0xA1Original value 0xE8 A >> 2 Resultant value 0000101 1 0010111 0 Say A = -95 Bitwise operators
  • 41. www.webstackacademy.com ‹#› Bitwise operator (8-bit system examples) Operator Example Same as Result Same as & 5 & 1 0000 0101 & 0000 0001 1 0000 0001 | 5 | 2 0000 0101 | 0000 0010 7 0000 0111 ~ ~ 5 ~0000 0101 -6 1111 1010 ^ 5 ^ 1 0000 0101 ^ 0000 0001 4 0000 0100 << 5 << 2 0000 0101 << 2 20 0001 0100 >> -5 >> 2 1111 1011 >> 2 -2 1111 1110 >>> -5 >>> 2 1111 1011 >>> 2 62 0011 1110
  • 42. www.webstackacademy.com ‹#› Class Work ● WAP to print bits of a given number ● WAP to count set bits in a given number
  • 43. www.webstackacademy.com ‹#› Web Stack Academy (P) Ltd #83, Farah Towers, 1st floor,MG Road, Bangalore – 560001 M: +91-80-4128 9576 T: +91-98862 69112 E: [email protected]