Top 25 interview questions of JavaScript
Top 25 interview questions of JavaScript
1. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
Strict mode is a way to introduce better error-checking into your code. When you use strict mode, you cannot use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible. You can enable strict mode by adding “use strict” at the beginning of a file, a program, or a function.
2.What is ECMAScript?
ECMAScript is a standard for making scripting languages which means that JavaScript follows the specification changes in ECMAScript standard because it is the blueprint of JavaScript.
ECMAScript standardized by the ECMA International standards organization in the ECMA-262 and ECMA-402 specifications.
3.What is NaN property in JavaScript?
NaN property represents “Not-a-Number” value. It indicates a value which is not a legal number.
typeof of a NaN will return a Number .
To check if a value is NaN, we use the isNaN() function,
4.What is negative Infinity?
Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.
5.What is the significance, and what are the benefits, of including 'use strict' at the beginning of a JavaScript source file?
The short and most important answer here is that use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice.
Some of the key benefits of strict mode include:
Makes debugging easier. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions, alerting you sooner to problems in your code and directing you more quickly to their source.
Prevents accidental global. Without strict mode, assigning a value to an undeclared variable automatically creates a global variable with that name. This is one of the most common errors in JavaScript. In strict mode, attempting to do so throws an error.
6.What is a prompt box in JavaScript?
A prompt box is a box which allows the user to enter input by providing a text box. The prompt() method displays a dialog box that prompts the visitor for input. A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value.
7.What is the reason for wrapping the entire content of a JavaScript source file in a function book?
This is an increasingly common practice, employed by many popular JavaScript libraries. This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.
Another feature of this technique is to allow for an easy alias for a global variable. This is often used in jQuery plugins.
8.What is Callback?
A callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘call back‘. In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.
9.How to create a cookie using JavaScript?
The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like this-
Syntax :
document.cookie = "key1 = value1; key2 = value2; expires = date";
10.Explain Higher Order Functions in JavaScript.
Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.
Higher order functions are a result of functions being first-class citizens in JavaScript.
11.Explain “this” keyword.
The “this” keyword refers to the object that the function is a property of.
The value of “this” keyword will always depend on the object that is invoking the function.
The silly way to understanding the this keyword is, whenever the function is invoked, check the object before the dot . The value of this . keyword will always be the object before the dot .
12.What is currying in JavaScript?
Currying is an advanced technique to transform a function of arguments n, to n functions of one or less arguments.
13.What is memoization?
Memoization is a form of caching where the return value of a function is cached based on its parameters. If the parameter of that function is not changed, the cached version of the function is returned.
By using memoization we can store(cache) the computed results based on the parameters. If the same parameter is used again while invoking the function, instead of computing the result, we directly return the stored (cached) value.
14.What is the difference between == and ===?
== is the abstract equality operator while === is the strict equality operator.
The == operator will compare for equality after doing any necessary type conversions.
The === operator will not do type conversion, so if two values are not the same type === will simply return false.
15.What are the new features in ES6 or ECMAScript 2015?
- Arrow Functions
- Classes
- Template Strings
- Enhanced Object literals
- Object Destructuring
- Promises
- Generators
- Modules
- Symbol
- Proxies
- Sets
- Default Function parameters
- Rest and Spread Operators
- Block Scoping with let and const
16.What are the scopes of a variable in JavaScript?
The scope of a variable is the region of your program in which it is defined.
JavaScript variable will have only two scopes.
Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.
Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
17.What are the Escape Characters in JavaScript?
We use escape characters backslash (\) while working with special characters, such as ampersands (&), apostrophes (‘), double quotes (“ ”), and single quotes (‘ ’).
Whatever enclosed within the escape characters gets displayed by the JavaScript.
Six additional escape characters are also available in JavaScript:
- \b – Backspace
- \f – Form feed
- \n – Newline
- \r – Carriage return
- \t – Horizontal tabulator
- \v – Vertical tabulator
These aren’t in anyway executed in the HTML or JS code.
These were originally designed for controlling fax machines, teletypes, and typewriters.
18.Which company developed JavaScript?
Netscape is the software company that developed JavaScript.
19.What is a prompt box?
A prompt box is a box that allows the user to enter input by providing a text box. A label and box will be provided to enter the text or number.
20.How can you convert the string of any base to an integer in JavaScript?
The parseInt() function is used to convert numbers between different bases. parseInt() takes the string to be converted as its first parameter. The second parameter is the base of the given string.
To convert 4F (or base 16) to integer, the code used will be -
parseInt ("4F", 16);
21.What are the disadvantages of using innerHTML in JavaScript?
If you use innerHTML in JavaScript, the disadvantage is
Content is replaced everywhere
We cannot use it like "appending to innerHTML
Even if you use +=like "innerHTML = innerHTML + 'html'" still the old content is replaced by html
The entire innerHTML content is re-parsed and builds into elements. Therefore, it's much slower
The innerHTML does not provide validation, and therefore we can potentially insert valid and broken HTML in the document and break it
22.What are the advantages of using External JavaScript?
Using External JavaScript in our code has many advantages.
These are stated below.
Separation of code is done.
Code Maintainability is easy.
The performance is better.
23.What is the use of the ‘debugger’ keyword in JavaScript code?
Using the ‘debugger’ keyword in the code is like using breakpoints in the debugger.
To test the code, the debugger must be enabled for the browser. If debugging is disabled for the browser, the code will not work. During debugging of the code, the remaining part should stop executing, before it goes to the next line.
24.What are Self Invoking Functions?
They are also known as ‘Immediately Invoked Function Expressions’ or ‘Self Executing Anonymous Functions’. These functions are invoked automatically in the code, hence they are named as ‘Self Invoking Functions’.
Usually, we define a function and invoke it, but if we want to execute a function automatically where it is explained, and if we are not going to call it again, we can use anonymous functions. And these types of functions have no name.
25.What is the difference between ‘null’ and ‘undefined’?
Both keywords represent empty values.
The differences are:
- In ‘undefined’, we will define a variable, but we won’t assign a value to that variable.
- On the other hand, in ‘null’ we will define a variable and assign the ‘null’ value to the variable.