JavaScript Class Notes: Operators

Updated: Jun 2

Mobiprep has created last-minute notes for all topics of Javascript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on javascript.

  1. JavaScript: Introduction

  2. Data Types

  3. Variables

  4. Operators

  5. Functions

  6. Conditional Statements and Loops

  7. Arrays

  8. Objects

  9. DOM Elements

  10. Cookies

  11. Pop Up Box

  12. Exception Handling

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. Explain arithmetic and assignment operators in JavaScript.

  2. What is the difference between == and ===?

  3. Explain comparison and logical operators in JavaScript.

  4. Write a note on Bitwise Operators in JavaScript.

  5. Write a note on string operators used in JavaScript.

  6. Write a note on Boolean operators in JavaScript.

  7. Explain the use of Infinity and NaN function in JavaScript.

  8. Explain the use of 'typeof' operator.

JavaScript Notes: Operators

Question 1. Explain arithmetic and assignment operators in JavaScript.

Assignment operator assigns a value to left operand from right operand. The equal (=), assigns the value of its right operand to its left operand. In example x = y assigns the value of y will be assigned to x.

There are compound assignment operators which are explained below.

Name

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

Left shift assignment

x <<= y

x = x << y

Right shift assignment

x >>= y

x = x >> y

​Bitwise AND assignment

​x & = y

x = x &amp; y

Bitwise OR assignment

x |= y

x = x | y

Bitwise XOR assignment

x ^= y

x = x ^ y

Logical AND assignment

x && = y

x &amp;&amp; (x = y)

Logical OR assignment

x ||= y

x || (x = y)

Arithmetic operators:

An arithmetic operator accepts numerical values as their operands and returns a single numerical value. The arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). In addition to the standard arithmetic operations (+, -, *, /), JavaScript provides the arithmetic operators listed below.

Operator

Description

Example

Remainder(%)

Binary operator. Returns the integer remainder of dividing the two operands.

12 % 5 returns 2.

Increament (++)

Unary operator. Adds one to its operand. If used as a prefix i. e. ++x, returns the value of its operand after adding one; if used as a postfix x++ returns the value of its operand before adding one.

If x is 4, then ++x sets x to 5 and returns 5, whereas x++ returns 4 and, only then, sets x to 5.

Decreament(--)

Unary operator. Subtracts one from its operand. The return value will act in similar way to increment operator.

If x is 4, then --x sets x to 3 and returns 3, whereas x-- returns 4 and, only then, sets x to 3.

Unary negation(-)

Unary operator. Returns the negation of its operand.

​If x is 3, then -x returns -3

Unary plus(+)

Unary operator. Attempts to convert the operand to a number, if it is not already.

+&quot;3&quot; returns 3. +true returns 1.

Exponentiation Operator (**)

Calculates the base to the exponent power, that is, base**exponent

2 ** 3 returns 8. 10 ** -1 returns 0.1.


 

Question 2. What is the difference between == and ===?


​==

===

Double equals (==) is equality comparison

Triple equals (===) are strict equality comparison

operator, which transforms the operands to the same type before comparison.

​operator, which returns false if there are different types and different content.

4 == 4 // true

4 === 4 // true

"4" == 4 //true

"4" === 4 //true

4 == "4" // true

4 === "4" // true

0 == false // true

var v2 = {"value": "key" };

v1 === v2 //false


 

Question 3. Explain comparison and logical operators in JavaScript.

1) Comparison operators — operators that compare values and return true or false. The operators include:

  • Less than (<) — returns true if the value on the left is less than the value on the right, otherwise it returns false.

  • Greater than (>) — returns true if the value on the left is greater than the value on the right, otherwise it returns false.

  • Less than or equal to (<=) — returns true if the value on the left is less than or equal to the value on the right, otherwise it returns false.

  • Greater than or equal to (>=) — returns true if the value on the left is greater than or equal to the value on the right, otherwise it returns false.

  • Equal to (===) — returns true if the value on the left is equal to the value on the right, otherwise it returns false.

  • Not equal to (!==) — returns true if the value on the left is not equal to the value on the right, otherwise it returns false.

2) Logical(Boolean) operators — operators that combine multiple boolean expressions or values and provide a single boolean output. The operators include:

  • && (and) — This operator will be truth (act like true) if and only if the expressions on both sides of it are true.

  • || (or) — This operator will be truth if the expression on either side of it is true. Otherwise, it will be false (act like false).

  • ! (not) _this returns opposite of the input.

 

Question 4. Write a note on Bitwise Operators in JavaScript.

  • Bitwise operators treat its operands as a group of 32-bit binary digits and perform actions. But, the result is shown as a decimal value.

  • Bitwise AND:

  • Bitwise AND & returns 1 if both operands are 1 else it returns 0.

  • Bitwise OR:

  • Bitwise OR | returns 1 if either of the corresponding bits of one operand is 1 else returns 0.

  • Bitwise XOR:

  • Bitwise XOR ^ returns 1 if the corresponding bits are different and returns 0 if the corresponding bits are the same.

  • Bitwise NOT:

  • Bitwise NOT ~ inverts the bit( 0 becomes 1, 1 becomes 0).

  • Bitwise SHIFT operators:

  1. LEFT SHIFT <<: I.e. shifts the elements to the left and insert 0 in the gap formed. For example, 15<<1 or 1111<<1 implies 1110 or 14.

  2. RIGHT SHIFT >>>: It is just the reverse of LEFT SHIFT, that is 15>>>1 or 1111>>>1 implies 0111 or 7.

  3. SIGNED RIGHT SHIFT >>: i.e. it returns the signed right shift. For example, 1111>>1 or 0111 or 7. Since the fourth bit is 0, positive.

 

Question 5. Write a note on string operators used in JavaScript.

The + operator, += operator used to concatenate strings.

Given that text1 = "Good ", text2 = "day", and text3 = text1 + text2 then text3=goodday.

 

Question 6. Explain the use of 'let' keyword in JavaScript.

Nan function :

Definition and Usage

The isNaN() function checks whether a value is a valid number

This function returns true if the value equal to Nan result . Otherwise it returns false.

This function is different from the.isNaN() method.

The global isNaN() function, converts the tested value to a Number, once conversion is done then tests it.


Example:

isNaN(123) //false

isNaN(-1.23) //false

isNaN(5-2) //false

isNaN(0) //false

isNaN('123') //false

isNaN('Hello') //true

isNaN('2005/12/12') //true

isNaN('') //false

isNaN(true) //false

isNaN(undefined) //true

isNaN('NaN') //true

isNaN(NaN) //true

isNaN(0 / 0) //true

isNaN(null) //false


Infinity function :

Division by 0 gives you another special value:

> 3/0

Infinity

You can’t perform any operation on positive and negative infinity against each other:

> Infinity - Infinity

NaN

It is still infinity:

> Infinity + Infinity

Infinity

> 5 * Infinity

Infinity


Question 7. Is there any ternary operator in JavaScript?

Yes, ‘?’ is the ternary operator. It is same as in C. That is, (condition? true: false).

For example, (12>2?1:0) returns 1.

 

Question 8. Explain the use of 'typeof' operator.

The uses of typeof operator are listed below:

  • Type of can be used to check whether a variable exists and it has a value.

  • The typeof operator used for checking that a value is neither undefined nor null.

  • It differentiate between objects and primitives.

  • It tells What is the type of a primitive value.

  • typeof check whether a value is a function.

 


Top 40 Javascript Interview Questions and Answers
.pdf
Download PDF • 1.15MB


 

Mobiprep is a one stop solution for all exam preparation needs. Get all exam prep resources at a single platform.

Back to Top

 


20 views0 comments

Recent Posts

See All