20
What are local (localy scoped) variables?
 
There is typically only one global scope, and each function defined has a nested (own) local scope. Every variable defined inside a local scope is a local variable.
 
function otherFunction() {
  var a = 2;
  alert(a);  // Will alert 2
}
 
21
What is Lexical Scope?
 
When we work with a nested functions, the inner functions can access the scope of the outer function. This is called Lexical Scope or Static scope.
 
var outerFunction = function () {
  var a = 1;
  alert(a); // inner scope, will alert 1
  var innerFunction = function () {
    alert(a); // outer scope, will alert 1
  };
};
 
22
What is a JavaScript Closure?
 
In JavaScript, when we work with a Lexical scope, a closure is an inner function that can access the enclosing (outer) function scope. The closure can access three scope chains, its own, outer function(s) scope and finally a global scope.
function outerFunction() {
  var nbr = 1; // nbr is a local scoped variable created by outerFunction()
  function innerFunction() { // innerFunction() is the inner function, also known as a closure
    alert(nbr); // closure can use variables declared in the outer function
  }
  innerFunction();    
};
outerFunction();
 
23
Can JavaScript produce Public and Private Scope?
 
In JavaScript, unlike other programming languages, there’s no such thing as a Public or Private scope. We can, however, emulate this feature using things like Closures.
 
function outerFunction() {
  var nbr = 1;
  function innerFunction() {
    alert(nbr);
  }
  innerFunction();    
};

innerFunction(); //  Will throw Uncaught ReferenceError: innerFunction is not defined
 
24
What is namespacing in JavaScript?
 
This is a process used for grouping the desired functions, variables, properties under a unique name or a namespace. Technically, this practice is usually used to create an object literal encapsulating various functions, variables, and properties, as not to collide with those created by other libraries.
 
This will in fact improved code modularity and reusability.
 
var someClass = {
  var1: '1',
  var2: '2',
  someFunction: function() {
    // function content
  }
};
 
25
What are anonymous functions?
 
Anonymous functions are functions that were declared without any named identifier. As such, it is usually not accessible after its initial creation.
 
(function() {
  console.log('foo');
})();

// or 

function() {
  console.log('foo');
};

 
A perfect example would be:
 
setTimeout(function() {
  console.log('foo');
}, 1000);
 
26
What is the importance of 'this' keyword?
 
In a few words, in JavaScript, the ‘this’ keyword can be described as a shortcut or a reference.
 
By default, this keyword refers to the outermost global object, the window. At the same time, all JavaScript functions and objects have (or can have) properties. When a function is executed, it will get the this property holding a value of the object that invokes said function.
 
The this reference always refers to an object and it’s usually used inside a function(s), although it can be used outside a function in the global scope.
 
var car = {
  color: "red",
  type: "BMW",
  showFullName: function() {
    console.log("Roger drives a " + this.color + " " + this.type); 
  }
}

car.showFullName(); 
 
27
What's the difference between Undefined and null?
 
In JavaScript, if a variable has been declared but has not yet been assigned a value, it is considered undefined.
 
var SomeVar;
alert(typeof SomeVar); //returns undefined
 
On the other hand, a null has an assignment value. It can also be assigned as a representation of no value.
 
var SomeVar= null;
alert(typeof SomeVar); //returns object
 
28
What's the difference between undeclared and undefined variables?
 
Undeclared variables are not declared nor they exist in a program. JavaScript will throw a runtime exception if a program tries to access it.
 
Unlike undeclared, undefined variables are declared in a program but don’t hold any value. JavaScript will return an undefined if a program tries to access it.
 
29
How can we check if something is an object?
 
The easiest way would be with typeof operator:
 
typeof somethingUnknown === "object"
 
But here comes a problem. Because null is also an object we’ll get a false positive, thus we also need to check for null value:
 
console.log((somethingUnknown !== null) && (typeof somethingUnknown === "object"));  // returns true
 
If we want to be especially careful, we must also check for Dates and Arrays.
 
30
Can two objects be equal?
 
Consider this:
 
var object1 = { foo: 'bar' };
var object2 = { foo: 'bar' };
console.log(object1 === object2); // will return false
 
We can’t directly compare two identical objects in JavaScript and except then to be equal. Two objects can be equal in JavaScript only if they refer to the same object, proof:
 
var object1 = { foo: 'bar' };
var object2 = object1;
console.log(object1 === object2); // will return true
 
The last example will return true because both objects reference the same object.
 
Unfortunately, there is no perfect way of comparing values of two objects. Fast and dirty way would be to stringify them before we compare them, like this:
 
var object1 = { foo: 'bar' };
var object2 = { foo: 'bar' };
JSON.stringify(object1) === JSON.stringify(object2) //would return true
 
31
What is an Object Prototype?
 
Every JavaScript objects have a prototype object and they inherit their properties and methods from that same prototype. For example, Objects created using an object literal inherit properties and methods from a prototype called Object.prototype.
 
Date objects inherit the Date.prototype, but at the same time also from the Object.prototype.
 
// This is an object prototype
function car(year, type, name) {
    this.year= year;
    this.type= type;
    this.name= name;
}
 
The new keyword is used to create new objects from the mentioned prototype:
 
var myCar = new car(2010, "Volkswagen", "Passat");
var friendsCar = new car(2012, "Mercedes", "A1");
 
We cannot add a new property to prototype object because the prototype is not an existing object.
 
// For this to work
myCar.color= "Red";

// Prototype must have that prototype  already defined
function car(year, type, name) {
    this.year= year;
    this.type= type;
    this.name= name;
    this.color= color;
}
 
But we can do it using prototype property:
 
function car(year, type, name) {
    this.year= year;
    this.type= type;
    this.name= name;
}

// This will work
car.prototype.color= "Red";
 
This also translates to functions:
function car(year, type, name) {
    this.year= year;
    this.type= type;
    this.name= name;
}

car.prototype.color= "Red";
 
car.prototype.getColor = function() {
    return this.color;
};

var myFather = new car(2010, "Mercedes", "A1");

console.log(myFather.getColor()); // will return Red
 
32
What is the difference between Arrays and Objects?
 
In JavaScript, arrays can have only numbered indexes so you should use them when you want the element names to be numbers. On the other hand, objects use named indexes so you should use them when you want the element names to be strings (text).
 
33
How can we create arrays in JavaScript?
 
There are two ways to create arrays in JavaScript. The first solution uses a more verbose method called new Array():
 
var a = new Array();
 
Or we can create an array using:
 
var a = [];
 
The second solution is safer than the first one. For example, there’s no way with new Array() to create an array with just one pre-specified number element in it, proof:
 
var a = new Array(5);     // an array pre-sized to 5 elements long
var b = new Array(5, 10); // an array with two elements in it
 
Ever current modern browsers will inform you to use [] notation instead of new Array().
 
Last but not least, to create a two or three-dimensional array we can use this syntax:
var a = [[]];
// or
var a = [[[]]];
 
34
Name at least one Array related method?
 
push() – is used to append one or more elements to the end of an Array.
unshift() – is used to prepend one or more elements to the beggining of an Array.
join() – joins all array elements into a string
pop() – removes the last element of an array
shift() – removes the first element of an array
splice() – add a new item to an array
var cars = ["BMW", "Renault","Mercedes"];
console.log(cars.join(" - "));             // will output: BMW - Renault - Mercedes
cars.push('Saab');
console.log(cars);                         // will output: ["BMW", "Renault", "Mercedes", "Saab"]
cars.pop();
console.log(cars);                         // will output: ["BMW", "Renault", "Mercedes"]
 
35
Does JavaScript support associative arrays?
 
The answer is yes and no at the same time. By default, JavaScript doesn’t support associative arrays. Associative arrays in JavaScript are actually treated as Objects, they are Objects.
 
While we can write them like this:
 
var car = new Array();
associativeArray['color'] = "red";
associativeArray['year'] = "2011";
associativeArray['type'] = "BMW";
 
In reality, they should look like this:
 
var associativeArray = {};  // denotes an Object is being created
associativeArray.color= "red";
associativeArray.year= "2011";
associativeArray.type= "BMW";
 
36
Is JavaScript case sensitive?
 
JavaScript is a highly case-sensitive language. You must use consistent capitalization of letters when working with anything JavaScript related, be it keywords, variables, functions, and any other identifiers.
 
document.getElementById('someID');  // Will work
document.getElementById('someid');  // Will fail
 
37
What is a window object?
 
A window object represents the browser’s window in is supported in all modern browsers. All global objects, functions, and variables are members of a window object. Global functions are methods of a window object, and global variables are properties of a window object.
 
It contains some well-known objects such as “document”, “history” etc. And yes, a document object is child of a window object, these two will do the same:
 
window.document.getElementById("someID");
document.getElementById("someID");
 
We can also use this object to measure a size of a window (height and width).
 
console.log(window.innerWidth);
console.log(window.innerHeight);
 
38
What is a document object?
 
A document object represents an HTML document loaded into a web browser. As such it’s the root node of the HTML document and main owner of all inner nodes; it also properties methods to access such node objects. Last but not least, it’s a child object of a window object.
 
For example, most commonly used document method is:
 
document.getElementById()
 
39
What is the BOM?
 
The BOM, also known as Browser Object Model allows JavaScript to “communicate” with the browser. BOM as such is not standardized so every browser can have own specific methods and properties. Thankfully, all modern browsers have implemented almost the same methods and properties.
 
Using the BOM, you can move the window, change status bar text, and perform other browser actions that do not directly relate to the page content.
 
Continue Reading

  • Hey Dragan,

    Great list, thanks!

    I would just like to point out a small typo at question #59: “typres”.

    In question #36 you probably meant to put the second line to someID instead of SomeID, right?

    Also, I would like to suggest two questions:
    1. Where is JavaScript actually executed?
    2. Can you run JavaScript on the server? [if the answer would be something like ‘sure man, just use Node.js’, then hire the applicant immediately ;)]

    Great posts, keep up the good work!

    • Thanks for your suggestions, I’ll write them today. Also I’ve fixed mentioned typos. 🙂

  • Rathnam Kolanu

    Hi,

    Thanks for taking to prepare List of questions and answers.
    Going through these questions will surely help me through javascript interviews.

    • First thank you very much. I will add around 50 more questions/answers over next few days. Then I will also offer it as a PDF download.

      • Rathnam Kolanu

        Thank you…
        Please post them as soon as possible… I am attending interviews on web development…..
        I would be waiting…