## Javascript Notes

JavaScript

Data Types

Variables

Operators

Functions

Conditional Statements and Loops

Arrays

Objects

DOM Elements

Cookies

Pop Up Box

Exception Handling

# Heading

Q

1

Explain arithmetic and assignment operators in JavaScript.

Ans

Assignment operators 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.

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.

Q

2

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

Ans

Q

3

Explain comparison and logical operators in JavaScript.

Ans

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

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

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

c. 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.

d. 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.

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

f. 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:

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

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

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

Q

4

Write a note on Bitwise Operators in JavaScript.

Ans

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:

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.

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

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.

Q

5

Write a note on string operators used in JavaScript.

Ans

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

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

Q

6

Explain the use of Infinity and NaN function in JavaScript.

Ans

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

Q

7

Is there any ternary operator in JavaScript?

Ans

Yes, ‘?’ is the ternary operator. It is same as in C. That is, (condition?true:false).For example, (12>2?1:0) returns 1.

Q

8

Explain the use of 'typeof' operator.

Ans

The uses of typeof operator are listed below:

Typeof 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.