Top 10 Most Asked JavaScript Interview Questions With Answers Part 08

By Muhammad Umair

Top 10 Most Asked JavaScript Interview Questions With Answers Part 08

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"

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;

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

Below are the main differences between null and undefined,

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

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

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

Below are the main differences between window and document,

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

By default window object is available implicitly on the page

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

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() {      
function goForward() {

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

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";

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

Below are the major differences between undeclared(not defined) and undefined variables,

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

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