Javascript Set1 of 10 Interview Questions

1) What are the basic types used in JavaScript?

Ans:
Primitive: String, Number, Boolean, Null, Undefined .

undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

Complex: Object (Arrays are Objects, Functions are Objects)

2) What are the ways to create object in JS?

1) // object constructor

var mango =  new Object ();
mango.color = "yellow";
mango.shape= "round";
mango.sweetness = 8;

mango.howSweetAmI = function () {
console.log("Hmm Hmm Good");
}
      

2) // object literal

      // This is an object with 4 items, again using object literal
var mango = {
color: "yellow",
shape: "round",
sweetness: 8,

howSweetAmI: function () {
console.log("Hmm Hmm Good");
}
}
   

3) // constructor pattern

function Fruit (theColor, theSweetness, theFruitName, theNativeToLand) {

    this.color = theColor;
    this.sweetness = theSweetness;
    this.fruitName = theFruitName;
    this.nativeToLand = theNativeToLand;

    this.showName = function () {
        console.log("This is a " + this.fruitName);
    }

    this.nativeTo = function () {
    this.nativeToLand.forEach(function (eachCountry)  {
       console.log("Grown in:" + eachCountry);
        });
    }


}

var mangoFruit = new Fruit ("Yellow", 8, "Mango", ["South America", "Central America", "West Africa"]);
   

4) // using prototype pattern

function Fruit () {

}

Fruit.prototype.color = "Yellow";
Fruit.prototype.sweetness = 7;
Fruit.prototype.fruitName = "Generic Fruit";
Fruit.prototype.nativeToLand = "USA";

Fruit.prototype.showName = function () {
console.log("This is a " + this.fruitName);
}

Fruit.prototype.nativeTo = function () {
            console.log("Grown in:" + this.nativeToLand);
}

var mangoFruit = new Fruit ();
   

3) Creating Arrays in JS

var a = new Array();
a[0] = 1.2;
a[1] = “Javascript”;
a[2] = true;
a[3] = { x:1, y:3};

var a = new Array(1.2,”Javascript”, true)

4) What is the difference between using call and apply to invoke a function?

var func = function(){
alert(‘hello!’);
};
func.apply();
vs
func.call();

The main difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly.
[A for apply, A for array]
[C for call, C for column of args]

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, …)

5) What do you understand by this keyword in JavaScript?
Ans: In JavaScript the this is a context-pointer and not an object pointer. It gives you the top-most context that is placed on the stack. The following gives two different results (in the browser, where by-default the window object is the 0-level context):

var obj = { outerWidth : 20 };

function say() {
alert(this.outerWidth);
}

say();//will alert window.outerWidth
say.apply(obj);//will alert obj.outerWidth

6) What would be the output of the following statements?

var object1 = { same: ‘same’ };
var object2 = { same: ‘same’ };
console.log(object1 === object2);
Ans: // Logs false, JavaScript does not care that they are identical and of the same object type.
When comparing complex objects, they are equal only when they reference the same object (i.e., have the same address). Two variables containing identical objects are not equal to each other since they do not actually point at the same object.

What would be the output of the following statements?

Code

var object1 = { same: ‘same’ };
var object2 = object1;
console.log(object1 === object2);

7) Consider the following statements and tell what would be the output of the logs statements?

var price1 = 10;
var price2 = 10;
var price3 = new Number(’10’); // A complex numeric object because new was used.
console.log(price1 === price2);
console.log(price1 === price3);
Ans:

console.log(price1 === price2); // Logs true.
console.log(price1 === price3); /* Logs false because price3
contains a complex number object and price 1
is a primitive value. */

8 ) Javascript Timing Events
It’s very easy to time events in JavaScript. The two key methods that are used are:

setInterval() – executes a function, over and over again, at specified time intervals
setTimeout() – executes a function, once, after waiting a specified number of milliseconds

9) How do you add a css class to an existing html element?
document.getElementById(“p1″).className += ” big”;

This will add the class big to the list of existing classes of element p1. If the “+=” was replaced by “=” then the class “big” will REPLACE all the existing classes of p1.

10) If you forget to declare a variable using “var” keyword inside a function what happens?
That variable will be treated as a global variable.

Leave a Reply