Back To Course

Introduction to JavaScript5 chapters | 12 lessons

Instructor:
*Vignesh Sivabalan*

Operators are essential in a programming language for performing various operations including arithmetic, logical, bitwise, conditional, and so on. This lesson explains various JavaScript Operators with clear examples.

The **Operators** in JavaScript are helpful in comparing values, assigning values, performing arithmetic and logical operations, and so on. Let us consider a very simple expression **3 * 7 = 21**. Here, 3 and 7 are ''operands'' and ''*'' is an operator. The following operators are supported by JavaScript:

- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Conditional Operators
- Assignment Operators
- String Operators
- Bitwise Operators
- typeof Operator
- delete Operator
- in Operator
- instanceof Operator

JavaScript provides support for the following arithmetic operators:

Let us consider two operands (variables) x and y.

Arithmetic Operation | Arithmetic Operator | Example | Result |
---|---|---|---|

Addition |
+ | x + y | If x = 7 and y = 8, then x + y = 15. |

Subtraction |
- | x - y | If x = 8 and y = 6, then x - y = 2. |

Multiplication |
* | x * y | If x = 3 and y = 3, then x * y = 9. |

Division |
/ | x / y | If x = 10 and y = 5, then x / y = 2. |

JavaScript provides support for the following Logical or Relational Operators. Let us assume two variables x = 5 and y = 10, then:

Logical Operation | Logical Operator | Example | Result | Explanation |
---|---|---|---|---|

Logical AND |
&& | if (x > 3 && y > 9) | TRUE | AND returns TRUE if both the conditions are TRUE. Here, 5 > 3 and 10 > 9 are both TRUE and hence result of the AND operation is ''TRUE''. |

Logical OR |
|| | if(x > 7||y > 8) | TRUE | OR returns TRUE even if one of the conditions is TRUE. Here, 5 > 7 is false but 10 > 8 is true and hence the result is ''TRUE''. |

Logical NOT |
! | !(x && y) | FALSE | ''!'' reverses the logical result of AND operation. The result of AND operation is ''TRUE'' and hence the result of NOT operator is ''FALSE''. |

The **Comparison Operators** are used to compare two or more values. The following comparison operators are supported by JavaScript.

Comparison Operator | Explanation | Example |
---|---|---|

Equal (==) |
Checks if the values of two operands are equal. If they are equal, it returns ''TRUE'' else ''FALSE'' | If x = 6 and y = 8, then (x == y) is FALSE. |

Not Equal to (!=) |
Checks if the values of two operands are not equal. If they are not equal, it returns ''TRUE'' else ''FALSE'' | If x = 6 and y = 8, then (x != y) is TRUE |

Greater than (>) |
Checks if the value of left operand is greater than right operand value. If the condition holds true, it returns ''TRUE'' else ''FALSE'' | If x = 6 and y = 8, then (x > y) is FALSE. |

Less than (<) |
Checks if the value of left operand is lesser than the right operand value. If the condition holds true, it returns ''TRUE'' else ''FALSE'' | If x = 6 and y = 8, then (x < y) is TRUE. |

Greater than or Equal to (>=) |
Checks if the values of two operands are equal or if the left operand is greater than the right. If either is true, it returns ''TRUE'' else ''FALSE'' | If x = 8 and y = 6, then (x >= y) is TRUE. |

Less than or Equal to (<=) |
Checks if the value of left operand is less than or equal to the value of right operand. If the condition is true, it returns ''TRUE'' else ''FALSE'' | If x = 6 and y = 6, then (x <= y) is TRUE. |

Based on certain conditions, the conditional or ternary operator assigns values to the variables.

**Syntax:**

variable_name = (conditions) ? val1 : val2

**Example:**

employable = (age > 18) ? ''Can be employed'' : ''Too young to be employed''

Comparison Operator | Example | Explanation | Result |
---|---|---|---|

Simple Assignment (=) |
x = y + z | Adds the value of y with z and assigns the value to x. | For example: If z = 3 and y = 2, then x = 5. |

Add and Assign (+=) |
x += y | x += y is equivalent to x = x + y. | For example: If x = 3 and y = 2, then x += y is 5. |

Subtract and Assign (-=) |
x -= y | x -=y is equivalent to x = x - y. | For example: If x = 3 and y = 2, then x -= y is 1. |

Multiply and Assign (*=) |
x *= y | x *= y is same as x = x * y. | For example: If x = 3 and y = 2, then x *= y is 6. |

Divide and Assign (/=) |
x /= y | x /= y is equivalent to x = x / y. | For example: If x = 3 and y = 2, then x /= y is 1.5. |

Modulo and Assign (%=) |
x %= y | x %= y is x = x % y; Modulo divides the values of y with x and assigns the remainder to x. | For example: If x = 3 and y = 2, then x %= y is 1. |

The string operators are + and += operators that concatenate strings.

Example: text_one = **''good''** and text_two = **''boy''** then text_one + text_two = **''good boy''**

These operators work with 32 bit numericals. Any number is first converted to 32 bits for operation and the result is again converted to a decimal number.

Bitwise Operator | Example | Equivalent to | Result | Decimal Number | Explanation |
---|---|---|---|---|---|

&(AND) |
y = 6 & 2 | 0110 & 0010 | 0010 | 2 | Bitwise AND returns 1 if both the operand's values are 1 else 0. |

|(OR) |
y = 6 | 2 | 0110 | 0010 | 0110 | 6 | Bitwise OR returns 1 if any one of the operand's value is 1. |

~(NOT) |
y = ~6 | ~0110 | 1001 | 9 | Bitwise NOT reveres the actual value 1 to 0 and 0 to 1. |

^(XOR) |
y = 6 ^ 2 | 0110 ^ 0010 | 1001 | 9 | Bitwise XOR reverses OR value. |

<<(Left shift) |
y = 6 << 1 | 0110 << 1 | 1100 | 12 | Left shift operator shift bits to left; here it shifts all bits of 0110 to left y a bit. |

>>(Right shift) |
y = 6 >> 1 | 0110 >> 1 | 0011 | 3 | Right shift operator shift bits to right; here it shifts all bits of 0110 to right by 1. |

This **typeof** operator will return the data type of an object, variable or an expression.

Example:

typeof 'Rosa' //returns string

typeof [8,9,11] //returns number

The **delete** JavaScript operator deletes one or more of the properties of an object.

Example:

var student = {name: 'Asha', age: 27, place: 'California'}

Delete student.place; // deletes the place property of the student

If the specified property or an array index exists in the object, the **in** operator will return TRUE, else FALSE.

Example:

var student = {name: 'Asha', age: 27, place: 'California'}

Here, the index value starts from 0. Index value of Asha is 0, 27 is 1 and so on.

4 in student //returns false

Asha in student //returns false as index value is needed

name in student //returns true

0 in student // returns true

It checks the type of specified object at run time and returns either true if an object type matches with the instance of some particular class, else false.

**Example:**

var today_date = new Date();

Here, today_date is an object whose type is Date.

if (today_date instanceof Date) //returns True

Operators are essential for performing arithmetic, assignment, bitwise, logical, ternary and many other operations. JavaScript provides operators such as assignment operators, arithmetic operators, bit wise operators, ternary operator, instanceof operator, typeof operator, logical operators, string operators, delete operator, in operator, and comparison operators which are discussed in this lesson with examples.

To unlock this lesson you must be a Study.com Member.

Create your account

Are you a student or a teacher?

Already a member? Log In

BackWhat teachers are saying about Study.com

Already registered? Login here for access

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

You are viewing lesson
Lesson
4 in chapter 3 of the course:

Back To Course

Introduction to JavaScript5 chapters | 12 lessons

- Introduction to HTML & CSS
- Introduction to JavaScript
- Computer Science 332: Cybersecurity Policies and Management
- Introduction to SQL
- Computer Science 203: Defensive Security
- JavaScript Language Basics
- JavaScript & HTML
- Error Handling, Debugging & Events in JavaScript
- HTML Elements & Lists
- Conditionals, Arrays & Loops in JavaScript
- Anti-Bullying Survey Finds Teachers Lack the Support They Need
- What is the ASCP Exam?
- ASCPI vs ASCP
- MEGA Exam Registration Information
- MEGA & MoGEA Prep Product Comparison
- PERT Prep Product Comparison
- MTLE Prep Product Comparison

- Dynamic Memory Allocation: Definition & Example
- How Culture Identity Impacts Early Childhood Development
- Arrays of Structures in C Programming
- Religion in the Neolithic Age
- Sensor Networks: Definition, Operation & Relationship
- Practical Application for C Programming: Creating Functions
- Publishing an Information Security Compliance Policy
- Quiz & Worksheet - ADHD in Students
- Quiz & Worksheet - Math in Daily Life
- Quiz & Worksheet - Questions About Chapter 8 of Lord of the Flies
- Quiz & Worksheet - The Arts in Early Childhood Curriculum
- Flashcards - Measurement & Experimental Design
- Flashcards - Stars & Celestial Bodies
- 12th Grade Math Worksheets
- Next Generation Science Standards

- Division: Homework Help Resource
- ORELA Business Education: Practice & Study Guide
- How to Make Effective Business Decisions
- Basics of Accounting
- DSST Principles of Statistics: Study Guide & Test Prep
- Writing Structure & Organization in Literature: Tutoring Solution
- Physical Science - Metamorphic Rocks - A Deeper Look: Help and Review
- Quiz & Worksheet - Francesco Redi
- Quiz & Worksheet - Bradford Protein Assay Protocol
- Quiz & Worksheet - Transitive Property
- Quiz & Worksheet - Charles by Shirley Jackson
- Quiz & Worksheet - Properties of Pure Substances

- How to Teach Reading Comprehension
- Johnny Tremain Chapter 3 Summary
- AP Biology Exam Date
- What Is the Syllabus of an Algebra I Course?
- AP Macroeconomics Exam: Tips for Short Free-Response Questions
- Common Core State Standards in Arkansas
- 2nd Grade Math Centers: Ideas & Activities
- 8th Grade Summer Reading List
- Essay Prompts, Rubric & Instructions for English Literature
- Best Free Online English Courses
- How to Pass a Science Test
- Life Skills and Guidance Resources for High School Students

- Tech and Engineering - Videos
- Tech and Engineering - Quizzes
- Tech and Engineering - Questions & Answers

Browse by subject