window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-63172957-1');
Sound
Awwwards
</html>
Created by potrace 1.16, written by Peter Selinger 2001-2019
Back to blog
JavaScript

Javascript Interview Questions & Answers (Part 6)

Article index:

What is a strict mode in javascript?

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This way it prevents certain actions from being taken and throws more exceptions. The literal expression “use strict”; instructs the browser to use the javascript code in the Strict mode.

Why do you need strict mode?

Strict mode is useful to write “secure” JavaScript by notifying “bad syntax” into real errors. For example, it eliminates accidentally creating a global variable by throwing an error and also throws an error for assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object.

How do you declare strict mode?

The strict mode is declared by adding “use strict”; to the beginning of a script or a function. If declare at the beginning of a script, it has global scope.

"use strict";
x = 3.14; // This will cause an error because x is not declared

and if you declare inside a function, it has local scope

x = 3.14; // This will not cause an error.
myFunction();

function myFunction() {
"use strict";
y = 3.14; // This will cause an error
}

What is the purpose of double exclamation?

The double exclamation or negation(!!) ensures the resulting type is a boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true. For example, you can test IE version using this expression as below,

let isIE8 = false;
isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // returns true or false

If you don’t use this expression then it returns the original value.

console.log(navigator.userAgent.match(/MSIE 8.0/)); // returns either an Array or null

Note: The expression !! is not an operator, but it is just twice of ! operator.

What is the purpose of delete operator?

The delete keyword is used to delete the property as well as its value.

var user= {name: "John", age:20};
delete user.age;

console.log(user); // {name: "John"}

What is 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"

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

What is 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

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

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.

What are the javascript data types?

Below is the list of javascript data types available

  • Number
  • String
  • Boolean
  • Object
  • Undefined

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

What are global variables?

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 a global variable

msg = "Hello" // var is missing, it becomes global variable


Back to blog

</html>
Wordpress Developer Loader, Web Developer Loader , Front End Developer Loader Jack is thinking