DEV Community

Vigowebs
Vigowebs

Posted on

JavaScript Interview Questions

1. What are the differences between null and undefined?
In JavaScript, undefined means, the value of the variable is not yet defined. And typeof undefined is also "undefined". We are getting undefined in JavaScript in some ways, like: declaring a variable without assigning any value to it, store a function return value to a variable but the function does not return anything, return statement does not return any values, a function parameter does not passed and the global variable undefined.

null means empty or non-existent value which is used to indicate “no value”. Even though typeof null returns object, null is primitive type and not an object.

2. What are the differences between == and ===?
The == will not check the type of the operands whereas === checks both type and value of the operands.

Other way of saying is, == will convert the operands to same type and then do the comparison. But === does not do any conversion. It will simply return false if any of them is in different type.

3. How can you check if the given variable is array or not?
We can use Array.isArray() to determine whether the passed value is an Array or not. However it will not run on IE8 and below. To support the old versions we can use, Object.prototype.toString.call(arr) === '[object Array]';

4. What is Scope of variable? What is Global scope and Local scope?
Scope means a set of rules to the compiler to look for a variable in the program or how the parser resolves the value of a variable.

In JavaScript, we have two kinds of scopes: Local and Global. If we have created a variable outside a function it is a global variable and can access from anywhere in our program. A variable declared inside a function has local scope. And they can be accessed from within the function, but not from outside of it.

5. What is prototype in Object? And what is constructor in Object?
JavaScript can be described as prototype-based language. Each object has prototype object which inherits methods and properties from another object.

Every function has a prototype property whose value is an object containing a constructor property. This constructor property points to the original constructor function.

6. What is typeof operator?
It is an unary operator, means it will have only one operand. It will return the data type of the operand, like "string", "number", or "boolean". The resulting type is always string.

Bonus: The typeof null return object even though it is not an object. From the first version of the JavaScript, the typeof checks the operand's type tag which is 1-3 bits (like, 000 for object, 1 for int and 100 for string) stored with values. null was the machine code NULL pointer or an object type tag, so it returns object.

To read more click here or download it on Android app

Bonus: Boost up your JavaScript Code samples skills to achieve in interviews or to learn try our JS Code Sample App

Top comments (0)