Commonly asked javascript interview questions
What you will learn here about javascript interview questions
- Commonly asked javascript interview questions
Commonly asked javascript interview questions
Following are the commonly asked javascript interview questions.
1)what is difference between == and ===
== and === both are comparison operator.
== operator compares only values not the data type
example:
if(10==”10″) /* this will return true */
/*if you observe the above line carefully, we are comparing number and string. Since number 10 and string “10” are the same in value
javascript will return true as result.*/
=== operator compares not only values but also the data type
example:
if(10===”10″) /* this will return false */
/*if you observe above line carefully, we are comparing number and string. Since number 10 and string “10” are same in value
but different in data type, javascript will return false as result because data type of 10 is number and data type of “10” is string.
*/
2)What is difference between = and ==
Example:
var name=”Arun” /* Assigns value Arun to the variable name*/== is the comparison operator used to compare two values.
example:
if(10==”10″) /* this will return true */
/*if you observe the above line carefully, we are comparing number and string. Since number 10 and string “10” are the same in value javascript will return true as result.*/
3)What is difference between >> and >>>
>> is the signed right shift operator. >> operator Shift bits of number right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
/*In javascript, each number is represented in 32 bit*/
/*bit number 31 is the signed bit and bits starting from position 0 to 30 are data bits*/-5>>2 /* -5 binary is the 2’s complement of 5*/00000000000000000000000000000101 /* binary representation of 5*/
11111111111111111111111111111010/*this is the 1’s complement of 5*/11111111111111111111111111111010
+ 1
——————————–
11111111111111111111111111111011 /* this is the binary representation of -5 */11111111111111111111111111111011>>2
———————————–
11111111111111111111111111111110 /*binary of -5 is shifted by two places to right and pushing two copies of the leftmost bit in from the left*//*11111111111111111111111111111110 2’s complement of this will be your result and since Leftmost bit(sign bit) is 1 result will be -ve */
10000000000000000000000000000001 /*1’s complement of 11111111111111111111111111111110*/
+ 1
——————————–
10000000000000000000000000000010 /*So final result is -2 since the leftmost bit is 1 */>>> is the Zero fill right shift operator. >>> operator Shift bits of number right by pushing zeros in from the left, and let the rightmost bits fall offExample:
/*In javascript, each number is represented in 32 bit*/
-5>>>2 /* -5 binary is the 2’s complement of 5*/00000000000000000000000000000101 /* binary representation of 5*/
11111111111111111111111111111010/*this is the 1’s complement of 5*/11111111111111111111111111111010
+ 1
——————————–
11111111111111111111111111111011 /* this is the binary representation of -5 */11111111111111111111111111111011>>2
———————————–
00111111111111111111111111111110 /*binary of -5 is shifted by two places to right and pushing two copies of the leftmost bit in from the left*//*00111111111111111111111111111110 2’s complement of this will be your result and since Leftmost bit(Sign bit) is 0 result will be +ve*/
01000000000000000000000000000001 /*1’s complement of 00111111111111111111111111111110*/
+ 1
——————————–
01000000000000000000000000000010 /*So final result is 1073741822 since the leftmost bit is 0 */
4)What are the selectors in Javascript
For detail about please visit following link
5)What is difference between id and class in HTML
id attribute is used to select only one element uniquely in HTML document where as
class attribute is used to select one or more elements.
You will not able to see big difference in CSS but you will see the big difference between these attributes when you use them in javascript.
6)What is difference between ‘ and “
In javascript both acts as string but ” acts as escape character for ‘ and ‘ acts as escape character for ”
7)Can we declare variable without var in javascript?
8)Can we use variables before declaration?
9)Can we redeclare variable in javascript?
10)What are javascript identifiers?
2. Identifier cannot start with a number.
3. Only two special character $ and _ can be used in identifiers.
4. Identifier cannot hava a space in between.
5. Keywords cannot be used as identifiers.
11)What are javascript data types?
- Primitive data type: One which holds one value at time is called as Primitive data type. Below primitive data types are listed
string
number
boolean
undefined
- Complex data type: List of complex data type is given below
- Function
- Object – Array, Object, null are the complex data type
12)Can we add String with Number in JavaScript?
13)What is the output of the following program
var C=10+10+” Jon Dow”
Output:
20 Jon Dow
14)What is the output of the following program
var C=”Jon Dow “+10+10
Output:
Jon Dow 1010
15)What is difference between null and undefined?
undefined is primitive data type where null is Complex data type.
When we don’t assign any value to variable, then default value assigned to variable is undefined.
example:
var c;
console.log(c);
Output:
undefined
var c=null;
console.log(c);
Output:
null
undefined==null // returns true
undefined===null // returns false
16)What is difference between let and var in javascript
The major difference between let and var is scope difference variables defined with var keyword in block has global scope where variables defined with let keyword in block has local scope.
Example of var keyword
var a=20;
{
var a=10;
}
console.log(a);
Output:
10
Example of let keyword
var a=20;
{
let a=10;
}
console.log(a);
Output:
20
17)What is difference between let and const in javascript
let and const both are same for block scope only difference is variables defined with const can’t be reassigned.
Example of const keyword
var a=20;
{
const a=10;
}
console.log(a);
Output:
20
18)Is javascript case sensitive?
Further Reading Suggestion:
How to Hire a Great JavaScript Developer