This book's goal is to help javascript frontend developers prepare for technical job interviews through a collection of carefully compiled questions.
- This Book will be soon completed and then it will be available to buy in paper form. If you want me to send you an early copy of this book, please add your name and email address in this Google Form.
- If you don't want to wait, you can buy Yuri's JavaScript Flashcards, a set of frontend interview questions sorted by popularity among interviewers printed on beautiful poker-size flashcards.
Answer
In JavaScript if you try to use a variable that doesn't exist and has not been declared, then JavaScript will throw an error var name is not defined
and the script will stop executing thereafter. But If you use typeof undeclared_variable
then it will return undefined
.
Before starting further discussion let's understand the difference between declaration and definition.
var x
is a declaration because we are not defining what value it holds yet, but we are declaring its existence and the need for memory allocation.
var x; // declaring x
console.log(x); // output: undefined
var x = 1
is both declaration and definition, here declaration and assignment of value happen inline for variable x—what we are doing is called "initialisation". In JavaScript both variable declarations and function declarations go to the top of the scope in which they are declared, then assignment happens—this series of events is called "hoisting".
A variable can be declared but not defined. When we try to access it, It will result undefined
.
var x; // Declaration
typeof x === 'undefined'; // Will return true
A variable can be neither declared nor defined. When we try to reference such variable then the result will be not defined
.
console.log(y); // Output: ReferenceError: y is not defined
http://stackoverflow.com/questions/20822022/javascript-variable-definition-declaration
if( x <= 100 ) {...}
if( !(x > 100) ) {...}
Answer
NaN <= 100
is false
and NaN > 100
is also false
, so if the
value of x
is NaN
, the statements are not the same.
The same holds true for any value of x that being converted to type Number, returns NaN
, e.g.: undefined
, [1,2,5]
, {a:22}
, etc.
This is why you need to pay attention when you deal with numeric variables. NaN
can’t be equal, less than or more than any other numeric value, so the only reliable way to check if the value is NaN
, is to use the isNaN()
function.
Answer
One of the drawbacks of declaring methods directly in JavaScript objects is that they are very memory inefficient. When you do that, a new copy of the method is created for each instance of an object. Here's an example:
var Employee = function (name, company, salary) {
this.name = name || "";
this.company = company || "";
this.salary = salary || 5000;
// We can create a method like this:
this.formatSalary = function () {
return "$ " + this.salary;
};
};
// Alternatively we can add the method to Employee's prototype:
Employee.prototype.formatSalary2 = function() {
return "$ " + this.salary;
}
//creating objects
var emp1 = new Employee('Yuri Garagin', 'Company 1', 1000000);
var emp2 = new Employee('Dinesh Gupta', 'Company 2', 1039999);
var emp3 = new Employee('Erich Fromm', 'Company 3', 1299483);
In this case each instance variable emp1
, emp2
, emp3
has its own copy of theformatSalary
method. However the formatSalary2
will only be added once to Employee.prototype
.
Answer
A closure is a function defined inside another function (called parent function) and as such it has access to the variables declared and defined within its parent function's scope.
The closure has access to the variables in three scopes:
- Variable declared in its own scope
- Variable declared in its parent function's scope
- Variable declared in the global namespace
var globalVar = "abc"; //Global variable
// Parent self-invoking function
(function outerFunction (outerArg) { // start of outerFunction's scope
var outerFuncVar = 'x'; // Variable declared in outerFunction's function scope
// Closure self-invoking function
(function innerFunction (innerArg) { // start of innerFunction's scope
var innerFuncVar = "y"; // variable declared in innerFunction's function scope
console.log(
"outerArg = " + outerArg + "\n" +
"outerFuncVar = " + outerFuncVar + "\n" +
"innerArg = " + innerArg + "\n" +
"innerFuncVar = " + innerFuncVar + "\n" +
"globalVar = " + globalVar);
// end of innerFunction's scope
})(5); // Pass 5 as parameter to our Closure
// end of outerFunction's scope
})(7); // Pass 7 as parameter to the Parent function
innerFunction
is a closure which is defined inside outerFunction
and consequently has access to all the variables which have been declared and defined within outerFunction
's scope as well as any variables residing in the program's global scope.
The output of the code above would be:
outerArg = 7
outerFuncVar = x
innerArg = 5
innerFuncVar = y
globalVar = abc
console.log(mul(2)(3)(4)); // output : 24
console.log(mul(4)(3)(4)); // output : 48
Answer
function mul (x) {
return function (y) { // anonymous function
return function (z) { // anonymous function
return x * y * z;
};
};
}
Here the mul
function accepts the first argument and returns an anonymous function which then takes the second parameter and returns one last anonymous function which finally takes the third and final parameter; the last function then multiplies x
, y
and z
, and returns the result of the operation.
In Javascript, a function defined inside another function has access to the outer function's scope and can consequently return, interact with or pass on to other functions, the variables belonging to the scopes that incapsulate it.
- A function is an instance of the Object type
- A function can have properties and has a link to its constructor method
- A function can be stored as a variable
- A function can be passed as a parameter to another function
- A function can be returned by another function