JavaScript Interview Questions (Part3)

How do you add an element at the beginning of an array?

You can use array.unshift() to insert elements at the begining of an array.

var arr = ["1", "12", "123"]; arr.unshift('1234','12345'); alert(arr);

How do you add an element at the end of an array?

You can use array.push() to insert elements at the end of an array.

var arr = ["1", "12", "123"]; arr.push('1234','12345'); alert(arr);

Why extending array is bad idea?

The problem is the global scope . Changing the behaviour of an object that will only be used by your own code is fine. But when you change the behaviour of something that is also used by other code there is a risk you will break that other code. Possible porblems:
  1. for..in might not work properly.
  2. Someone else might also want to extend Array with the same function name.
  3. It might not work properly in every browser.

How do you check if an object is an array or not?

Object.prototype.toString returns the value of the object's internal [[Class]] property. Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString()method is inherited by every object descended from Object. If this method is not overridden in a custom object,toString() returns '[object type]', where type is the object type.
var arr = ["1", "12", "123"]; if( Object.prototype.toString.call( arr ) === '[object Array]' ) { alert( 'Array!' ); }else { alert( 'Not an Array!' ); }

How to empty an array in JavaScript?

var list = [1, 2, 3, 4]; list = [];
This code will set the array list to a new empty array. This is perfect if you don't have any references to the original array list anywhere else because this actually creates a brand new (empty) array.

If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero.

var list = [1, 2, 3, 4]; list.length = 0;

Can you assign an anonymous function to a variable?

Yes. You can assign JavaScript function to variables, which means you can assign anonymous JavaScript functions to variables.

How can you get the type of arguments passed to a function?

You can use the typeOf function to get the type of arguments passed to a function.

function getType(arg){ alert(typeof arg); } getType('foo'); //return string getType(123); //return number

What is the function of delete operator?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically. This operator can also delete variables which are not declared with the var statement.
var Student = { firstName: "John", Age: 25 } alert(Student.firstName); // Return: "John" delete Student.firstName; alert(Student.firstname); // Return: undefined

Difference between private, public and static variable?

  1. Public variables can be accessed by all the members of the owner as well as other objects that can access the owner.
  2. Private variables can be accessed by all the members (functions and variables) of the owner object but not by any other object.
  3. Static variables are come into existence as soon as a class come into existence. It doesn't matter whether the class has any objects or not, static variables would be there.

What is differential inheritance?

JavaScript uses an inheritance model called "differential inheritance" . It is a common inheritance model used by prototype-based programming languages. What that means is that methods aren't copied from parent to child. Instead, children have an "invisible link" back to their parent object.

How to Object.entries( ) In JavaScript ?

The entries() method returns an Array Iterator object with key/value pairs, in the same order as that provided by a for...in loop.

const Days = { Day1: 'Sunday', Day2: 'Monady', Day3: 'Tuesday' }; const entries = Object.entries(Days); alert(entries);

How would you compare two objects in JavaScript?

function strictlyEqual(a, b) { if (a === b) return true; }

We're checking if a and b are strictly equal (meaning, "referring to exactly the same thing").

What is the use of blur function in JavaScript?

The blur() method is used to remove focus from an element.

document.getElementById('myField').onblur();
It has no effect on an element that is not the active element in the document. If you use the blur method for the active element, then it loses the active state and fires the onblur event.

What is NaN?

NaN is just a special value to return as a result of conversion to number instead of throwing an exception, if type conversion has failed.

How to set the cursor to wait in JavaScript?

window.document.body.style.cursor = "wait";

What is the difference between ViewState and SessionState?

ViewState is specific to a page in a session.

SessionState is specific to user specific data that can be accessed across all pages in the web application.

How to enabled '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 strict context prevents certain actions from being taken and throws more exceptions.

Just add "use strict"; at the top of your code, before anything else.

'use strict'; var str = "Hello World"; document.write(str);

Is there automatic type conversion in JavaScript?

JavaScript is a loosely typed language. Whenever an operator or statement is expecting a particular data-type, it will automatically convert the data to that type.

example
var numStr = '5'; var x = numStr*1; var y = +numStr; typeof(numStr); //"string" typeof(x); //"number" typeof(y); //"number"

In Javascript are calculations with fractional numbers guaranteed to be precise?

Calculations with fractional numbers are not guaranteed to be precise in Javascript.

Moreover, that depends on what kind of calculations you're doing.

  1. If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.
  2. If you just don't want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
  3. If you have no decimal datatype available, an alternative is to work with integers, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.

What will happen if an infinite while loop is run in Javascript?

Your computer most likely locked up because the infinite loop made the browser you were using unstable.

What is unshift method in JavaScript?

The unshift() method adds new items to the beginning of an array and returns the new length of the array.

var days = ["Sunday", "Monday"]; document.write(days); days.unshift("Tuesday","Wednesday"); document.write(days);