Top 10 Most Asked JavaScript Interview Questions With Answers Part 08

Top 10 Most Asked JavaScript Interview Questions With Answers Part 08

Question No 1:

What is the typeof operator?

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.

typeof "John Abraham"; // Returns "string"    
typeof (1 + 2); // Returns "number"

Question No 2:

What is undefined property?

The undefined property indicates that a variable has not been assigned a value, or not declared at all. The type of undefined value is undefined too.

var user; // Value is undefined, type is undefined    console.log(typeof user); //undefined

Any variable can be emptied by setting the value to undefined.

user = undefined;

Question No 3:

What is a null value

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.

var user = null;    
console.log(typeof user); //object

Question No 4:

What is the difference between null and undefined

Below are the main differences between null and undefined,

Null

It is an assignment value which indicates that the 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

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

Question No 5:

What is eval

The eval() function evaluates JavaScript code represented as a string. The string can be a JavaScript expression, variable, statement, or sequence of statements.

console.log(eval("1 + 2")); //  3

Question No 6:

What is the difference between window and document

Below are the main differences between window and document,

Window

It is the root level element in any web page | It is the direct child of the window object. This is also known as Document Object Model(DOM)

You can access it via window. document or document.

It provides methods like getElementById, getElementsByTagName, createElement etc

Document

By default window object is available implicitly on the page

It has methods like alert(), confirm() and properties like document, location

Question No 7:

How do you access history in javascript?

The window.history object contains the browser’s history. You can load previous and next URLs in the history using back() and next() methods.

function goBack() {      
window.history.back();
}
function goForward() {
window.history.forward();
}

Note: You can also access history without the window prefix.

Question No 8:

How do you detect caps lock key is turned on or not

The mouseEvent getModifierState() is used to return a boolean value that indicates whether the specified modifier key is activated or not. The modifiers such as CapsLock, ScrollLock and NumLock are activated when they are clicked and deactivated when they have clicked again.

Let’s take an input element to detect the CapsLock on/off behaviour with an example,

function enterInput(e) {        
var flag = e.getModifierState("CapsLock");
if (flag) {
document.getElementById("feedback").innerHTML = "CapsLock activated";
} else {
document.getElementById("feedback").innerHTML =
"CapsLock not activated";
}
}

Question No 9:

What is isNaN

The isNaN() function is used to determine whether a value is an illegal number (Not-a-Number) or not. i.e, This function returns true if the value equates to NaN. Otherwise, it returns false.

isNaN("Hello"); //true    
isNaN("100"); //false

Question No 10:

What are the differences between undeclared and undefined variables

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 are declared in the program but have not been assigned any value.

If you try to read the value of an undefined variable, an undefined value is returned.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Umair

Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python