JS Interview Questions
JS Interview Questions
The primary reason to use an IIFE is to obtain data privacy because any variables declared
within the IIFE cannot be accessed by the outside world. i.e, If you try to access variables
from the IIFE then it throws an error as below,
(function () {
var message = "IIFE";
.log(message);
})();
console.log(message); //Error: message is not defined
• What is Hoisting
o Hoisting is a JavaScript mechanism where variables, function declarations and classes are
moved to the top of their scope before code execution. Remember that JavaScript only
hoists declarations, not initialisation. Let's take a simple example of variable hoisting,
o console.log(message); //output : undefined
var message = "The variable Has been hoisted";
o The above code looks like as below to the interpreter,
var message;
console.log(message);
message = "The variable Has been hoisted";
o In the same fashion, function declarations are hoisted too
message("Good morning"); //Good morning
function message(name) {
console.log(name);
}
o This hoisting makes functions to be safely used in code before they are declared.
• What is scope in javascript
o Scope is the accessibility of variables, functions, and objects in some particular part of
your code during runtime. In other words, scope determines the visibility of variables and
other resources in areas of your code.
• What is web storage
o Web storage is an API that provides a mechanism by which browsers can store key/value
pairs locally within the user's browser, in a much more intuitive fashion than using cookies.
The web storage provides two mechanisms for storing data on the client.
▪ Local storage: It stores data for current origin with no expiration date.
▪ Session storage: It stores data for one session and the data is lost when the
browser tab is closed.
• What is the main difference between localStorage and sessionStorage
o LocalStorage is the same as SessionStorage but it persists the data even when the browser
is closed and reopened(i.e it has no expiration time) whereas in sessionStorage data gets
cleared when the page session ends.
• What is typeof operator
o You can use the JavaScript typeof operator to find the type of a JavaScript variable. It
returns the type of a variable or an expression.
• What is undefined property
o The undefined property indicates that a variable has not been assigned a value, or declared
but not initialized at all. The type of undefined value is undefined too.
• What is null value
o The value null represents the intentional absence of any object value. It is one of
JavaScript's primitive values. The type of null value is object. You can empty the variable by
setting the value to null.
• What is the difference between null and undefined
o Null
▪ It is an assignment value which indicates that variable points to no object.
▪ Type of null is object
▪ The null value is a primitive value that represents the null, empty, or non-existent
reference.
▪ Indicates the absence of a value for a variable
▪ Converted to zero (0) while performing primitive operations
o Undefined
▪ It is not an assignment value where a variable has been declared but has not yet
been assigned a value.
▪ Type of undefined is undefined
▪ The undefined value is a primitive value used when a variable has not been assigned
a value.
▪ Indicates absence of variable itself
▪ Converted to NaN while performing primitive operations
• What is eval
o The eval() function evaluates JavaScript code represented as a string. The string can be a
JavaScript expression, variable, statement, or sequence of statements.
o console.log(eval("1 + 2")); // 3
• What are the differences between undeclared and undefined variables
o Below are the major differences between undeclared(not defined) and undefined variables,
▪ Undeclared
• These variables do not exist in a program and are not declared
• If you try to read the value of an undeclared variable, then a runtime error is
encountered
▪ Undefined
• These variables declared in the program but have not assigned any value
• If you try to read the value of an undefined variable, an undefined value is
returned.
• What are global variables
o Global variables are those that are available throughout the length of the code without any
scope. The var keyword is used to declare a local variable but if you omit it then it will
become global variable
o msg = "Hello"; // var is missing, it becomes global variable
• What is NaN property
o The NaN property is a global property that represents "Not-a-Number" value. i.e, It
indicates that a value is not a legal number. It is very rare to use NaN in a program but it can
be used as return value for few cases
o Math.sqrt(-1);
parseInt("Hello");
• What is an event flow
o Event flow is the order in which event is received on the web page. When you click an
element that is nested in various other elements, before your click actually reaches its
destination, or target element, it must trigger the click event for each of its parent elements
first, starting at the top with the global window object.
o There are two ways of event flow
▪ Top to Bottom(Event Capturing)
▪ Bottom to Top (Event Bubbling)
• What is event bubbling
o Event bubbling is a type of event propagation where the event first triggers on the innermost
target element, and then successively triggers on the ancestors (parents) of the target
element in the same nesting hierarchy till it reaches the outermost DOM element(i.e, global
window object).
• What is event capturing
o Event capturing is a type of event propagation where the event is first captured by the
outermost element, and then successively triggers on the descendants (children) of the
target element in the same nesting hierarchy till it reaches the innermost target DOM
element.
• Is JavaScript a case-sensitive language
o Yes, JavaScript is a case sensitive language. The language keywords, variables, function &
object names, and any other identifiers must always be typed with a consistent
capitalization of letters.
• What is the use of stopPropagation method
o The stopPropagation method is used to stop the event from bubbling up the event chain.
• What is JSON
o JSON (JavaScript Object Notation) is a lightweight format that is used for data
interchanging. It is based on a subset of JavaScript language in the way objects are built in
JavaScript.
• What is the purpose JSON stringify
o When sending data to a web server, the data has to be in a string format. You can achieve
this by converting JSON object into a string using stringify() method.
var userJSON = { name: "John", age: 31 };
var userString = JSON.stringify(userJSON);
console.log(userString); //"{"name":"John","age":31}"
• How do you parse JSON string
o When receiving the data from a web server, the data is always in a string format. But you
can convert this string value to a javascript object using parse() method.
var userString = '{"name":"John","age":31}';
var userJSON = JSON.parse(userString);
console.log(userJSON); // {name: "John", age: 31}
• How do you check if a string starts with another string
o You can use ECMAScript 6's String.prototype.startsWith() method to check if a string
starts with another string or not. But it is not yet supported in all browsers. Let's see an
example to see this usage,
"Good morning".startsWith("Good"); // true
"Good morning".startsWith("morning"); // false
• How do you trim a string in javascript
o JavaScript provided a trim method on string types to trim any whitespaces present at the
beginning or ending of the string.