1. Write examples for addition and subtraction operators in JavaScript?
The
Example:
The
Example:
2. What are the different Operators in JavaScript?
Assignment Operators
Name | Shorthand operator | Meaning |
---|---|---|
Assignment | x = f() | x = f() |
Addition assignment | x += f() | x = x + f() |
Subtraction assignment | x -= f() | x = x - f() |
Multiplication assignment | x *= f() | x = x * f() |
Division assignment | x /= f() | x = x / f() |
Remainder assignment | x %= f() | x = x % f() |
Comparison operators
Operator | Description |
---|---|
Equal (==) | Returns true if the operands are equal. |
Not equal (!=) | Returns true if the operands are not equal. |
Strict equal (===) | Returns true if the operands are equal and of the same type. |
Strict not equal (!==) | Returns true if the operands are of the same type but not equal, or are of a different type. |
Greater than (>) | Returns true if the left operand is greater than the right operand. |
Greater than or equal (>=) | Returns true if the left operand is greater than or equal to the right operand. |
Less than (<) | Returns true if the left operand is less than the right operand. |
Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. |
Arithmetic operators
Operator | Description |
---|---|
+ | Adds the operands |
- | Subtracts the operands |
* | Multiplies the operands |
/ | Divides the operands |
Logical Operators
Operator | Usage | Description |
---|---|---|
Logical AND (&&) | expr1 && expr2 | Returns expr1 if it can be converted to false else returns expr2. Thus, when used with Boolean values, && returns true if both operands are true else returns false. |
Logical OR (||) | expr1 || expr2 | Returns expr1 if it can be converted to true else returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false. |
Logical NOT (!) | !expr | Returns false if its single operand that can be converted to true else returns true. |
Conditional (ternary) operator
It takes three operands.
Syntax:
If a condition is true, the operator has the value of val1. Otherwise, it has the value of val2.
3. Why does comparing two similar objects return false in JavaScript?
- JavaScript compares objects and primitives differently.
- In primitives, it compares them by value while in objects it compares them by reference or the address in memory where the variable is stored.
Example:
- That's why the first console.logstatement returnsfalseand the secondconsole.logstatement returnstrue. a and c have the same reference while a and b are not.
4. What is the difference between loose equal to (==) and strict equal to (===)?
Loose equal to (==)
Loose equality compares two values for equality but doesn't compare type of values.
Strict equal to (===)
Strict equality compares two values for equality including type of values.
Example:
5. What does JavaScript use instead of == and !=?
JavaScript uses
6. What is a Switch case?
A
It evaluates an expression. The value of the expression is then compared with the values of each case in the structure. If there is a match, the associated block of code is executed.
Syntax:
The
breakkeyword stops the execution.The
defaultkeyword specifies some code to run if there is no case match. There can only be onedefaultkeyword in aswitch.
7. What are the differences between if...else and switch statements?
basis for comparison | if...else | switch |
---|---|---|
Basic | Which statement will be executed depend upon the output of the expression inside if statement | Which statement will be executed is decided by user |
Expression | if...else statement uses multiple statement for multiple choices | switch statement uses single expression for multiple choices |
Testing | if...else statement test for equality as well as for logical expression | switch statement test only for equality |
Evaluation | if statement evaluates integer, character, pointer or floating-point type or boolean type | switch statement evaluates only character or integer value |
Sequence of Execution | Either if statement will be executed or else statement is executed | switch statement execute one case after another till a break statement is appeared or the end of switch statement is reached |
Default Execution | If the condition inside if statements is false, then by default the else statement is executed if created | If the condition inside switch statements does not match with any of cases, for that instance the default statements is executed if created |
8. What are the Conditional Statements in JavaScript?
The Conditional Statements allow you to execute a block of code only when a specific condition is true.
If...Else Statement
Switch Statement
9. What does the || operator do?
- The ||orLogical ORoperator finds the first truthy expression in its operands and returns it. If all values are falsy, the last operand gets returned.
Example:
10. What is the output of 10+"10" in JS?
11. What is the output of 10+10 in JS?
12. Explain break and continue statements?
Break statement | Continue statement |
---|---|
The break statement is used to jump out of a loop. It can be used to jump out of a switch() statement. It breaks the loop and continues executing the code after the loop. | The continue statement jumps over one iteration in the loop. It breaks iteration in the loop and continues executing the next iteration in the loop. |
13. What is the difference between the Spread Operator and Rest Parameter?
Spread Operator: The Spread Operator is used to unpack an iterable (e.g. an array, object, etc.) into individual elements.
Example:
Rest Parameter: With Rest Parameter, we can pack multiple values into an array.
Example: