I am writing this list hoping to aggregate only most pertinent JavaScript interview questions and answers. Last few weeks I was participating as an interviewer (company name is not significant) on the topic of PHP and mobile development. I was amazed how many people still don’t understand what’s JavaScript and what is its role in web development. Let me be clear, I’m talking about a novice developers, seniors should know this in their sleep.
 
If possible, please participate with new questions, just post them in the comments section below. I will also open a Reddit post so we can discuss everything said here.
 
 

Note: If this tutorial was helpful, need further clarification, something is not working or do you have a request for another Ionic post? Furthermore, if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here, again leave me a comment below. I'm here to help you, I expect the same from you. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com, or follow and mention me on twitter (@gajotres). Thanks and have a nice day!

PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.


 
1
What is JavaScript, what about history?
 
JavaScript, some also know it as ECMAScript, is a dynamic programming language used in a web development. It is an integral part of web browsers, and it allows, among other things, better user interaction, browser control, client-server communications (synchronous and asynchronous), alterations of displayed DOM content.
 
Although it was first introduced under a different name (Mocha), during its initial release it was officially named LiveScript. Developed by Netscape, the first version was available in Navigator 2.0 (September 1995), but it was renamed JavaScript in version 2.0B3.
 
2
How's JavaScript related to Java and JScript?
 
Only two things connects JavaScript with Java, mainly history, and some syntax. During the initial development, Netscape wanted a lightweight language that can complement Java computer programming language, and at the same time, appeal to nonprofessional programmers. This choice is also characterized as a Netscape marketing ploy, excellent one if I may say.
 
Let’s not forget similar syntax, for example, JavaScript offers this function that parses a string and returns an integer:
 
var a = parseInt("10");
 
While Java uses this syntax:
 
int x = Integer.parseInt("9");
 
Another example is try-catch-throw block.
 
On the other hand, JScript was created by Microsoft as a port of Netscape’s JavaScript. Unfortunately, it included many differences which led to infamous ‘best viewed in Netscape’ and ‘best viewed in Internet Explorer’ war. Take a wild guess who won this competition.
 
3
Enumerate different groups of data types used in JavaScript?
 
JavaScript knows only of two groups of data types, Primitive and Reference types. Numbers, booleans, nulls, and undefined types are primitive types, and they hold a fixed size in memory, except undefined. Everything else is considered Reference types, and they don’t have a fixed size.
 
4
What is the use of the Typeof operator?
 
Typeof operator returns a string representation of checked data types.
 
typeof 14 === 'number';
typeof "bla" === 'string';
typeof true === 'boolean';
typeof Symbol() === 'symbol'
typeof undefined === 'undefined';
typeof {foo:bar} === 'object';
typeof [1, 2, 3] === 'object';
typeof new Date() === 'object';
typeof function(){} === 'function';
typeof null === 'object';
 
5
Enumerate different data types used in JavaScript?
 
  • String
  • Number
  • Symbol (new in ECMAScript 2015)
  • Boolean
  • Function
  • Object
  • Null
  • Undefined
We could also count Date and Array though they’re considered Objects when checked with typeof operator. null is also considered an Object since the beginning of JavaScript.
 
6
Do we need to care about JavaScript data type conversions?
 
No, we don’t need to care about JavaScript data type conversions. Such actions are done automatically though not always in a sensible way.
 
For example, if we try to add two numbers and one of them is a string representation of a number, JavaScript will concatenate them. It will handle them like two strings, not two numbers.
 
1 + 2 + "3"   //  will give  33 
1 + 2 + +"3"  //  will give  6
"1" + 2 + 3   //  will give  123
+"1" + 2 + 3  //  will give  6
 
On the other hand, this will work just fine:
 
var s = '5';  
console.log(s); // will return 5, Typeof string
var a = s*1;  
console.log(a); // will return 5, Typeof number
var b = +s;   
console.log(b); // will return 5, Typeof number
 
7
Name any JavaScript function used for data conversion?
 
Number(); Object to Number
parseFloat(); String to floating point number
parseInt(); String to integer
String(); Object to string
 
8
How can we comment code in JavaScript?
 
Single line comments:
 
// var s = '5';  
 
Multi-line comments:
 
/* var s = '5';  
console.log(s); */
 
9
What is the use of isNaN() and isFinite()?
 
Function isNan translates to “is not a number”. As it name stats this function will return true if argument is not a number and vice versa.
 
console.log(isNaN("123"));   // will return false
console.log(isNaN("Hello")); // will return true
 
On the other hand, the isFinite function returns true if argument is a finite number. It will return false if an argument is not a number or is a positive/negative infinite number.
 
console.log(isNaN("123"));   // will return true
console.log(isNaN("Hello")); // will return false
 
10
What is the use of eval() and why is it dangerous?
 
The eval() method evaluates and executes JavaScript code represented as a string. While great on paper, this method can cause great security problems if not handled properly. For example, it can be used for code injection. So technically, while not inherently dangerous, don’t use eval needlessly!
 
console.log(eval("2 + 2")); // will return 4
 
11
What are the decodeURI() and encodeURI() methods?
 
encodeURI() is used to encode (convert) URL into hex representation of characters. And decodeURI() is used to decode the encoded URL back to the classic form.
 
var uri="https://www.google.hr/?gws_rd=ssl&error=Some stupid error";
console.log(encodeURI(uri));    // will output: https://www.google.hr/?gws_rd=ssl&error=Some%20stupid%20error
console.log(decodeURI(uri));    // will output: https://www.google.hr/?gws_rd=ssl&error=Some stupid error
 
12
Define unescape() and escape() methods?
 
escape() method will encode a string thus making it portable, so it can be transferred to any remote device that supports ASCII characters. unescape() will reverse this process.
 
console.log(escape("This string needs some 'decoding'"));                  // will output: This%20string%20needs%20some%20%27decoding%27
console.log(unescape("This%20string%20needs%20some%20%27decoding%27"));    // will output: This string needs some 'decoding'
 
13
What are escape characters?
 
When we’re working with a special characters that can brake our code like single quotes, double quotes, apostrophes or ampersands, we need to use a backslash before mentioned special characters. Backslash will tell JavaScript not to consider this character as a special one.
 
console.log('This is a \'special\' route');                  // will output: This is a 'special' route
 
Escape characters also work with inline JavaScript.
 
14
Can we brake JavaScript code into several lines?
 
If we’re not working with strings, JavaScript will ignore break lines:
 
var foo=1, 
 
bar=
 
foo + 2;
console.log(bar); // will output 3
 
15
What is Scope in JavaScript?
 
When talking about the JavaScript, the scope is the current context of your code. The scopes are globally or locally defined. It specifies “from where” and “if you” can assess some variable(s). It is also one of the most important aspects of the JavaScript, to understand where and when variables (including functions) are accessible. Global scope variable example:
 

var a = 1;

function one() {
  alert(a); // Will alert 1
}

 
Local scope variable example:
 

var a = 1;

function someFunction(a) {
  alert(a);  // Will alert 1
}

function otherFunction() {
  var a = 2;
  alert(a);  // Will alert 2
}

 
16
Is JavaScript a pass-by-reference or pass-by-value language?
 
All primitive data types (string, number, etc.) are passed by value where objects are passed by reference.
 
This way you can change a property of the passed object, but you can’t assign a new object instead of a passed one.
 
function testArguments(primitive, obj1, obj2)
{
  primitive = primitive * 10;
  obj1.item = "changed";
  obj2 = {item: "changed"};
}

var primitive = 10;
var obj1 = {item: "unchanged"};
var obj2 = {item: "unchanged"};

testArguments(primitive, obj1, obj2);

console.log(primitive);  // will return 10
console.log(obj1.item);  // will return changed 
console.log(obj2.item);  // will return unchanged
 
17
What is variable and function hoisting in JavaScript?
 
Variable and Function hoisting is one unusual feature of JavaScript. Both variables and functions can be available before they declared.
 
Variables:
 
var myvar = 'my value'; 
  
(function() { 
  alert(myvar); // undefined 
  var myvar = 'local value'; 
})();
 
Functions:
 
// Outputs: "Yes!"
isItHoisted();

function isItHoisted() {
    console.log("Yes!");
}
 
JavaScript treats variables which will be declared later on in a function differently than non-declared variables. Technically, JavaScript will look ahead to find all the variable declarations and “hoists” them to the top of the function. To avoid possible problems it is advised to declare all variable at the top of the function(s).
 
More complex example:
 

var foo; //declaration

foo = "bar"; // initialization

var foo; // Another declaration of foo is hoisted and the value is set to undefined.

foo = "bar"; // The value is now again "bar")

function getFoo() {
    var foo; // Another declaration of foo is hoisted and the value is set to undefined.

    console.log(foo); // will return undefined

    foo = "bar"; 

    console.log(foo); // The output is "bar"
}
 
18
What are global (globaly scoped) variables?
 
Global variables are no more different than local variables, but at the same time, are available globally to every part of the code. Strictly, they don’t have a scope. When declaring global variables the var keyword is omitted.
 
globalVar= 'Heh';    // Global variable

function someFunction() {
    var localVar= 'Meh';  // Local variable
}
 
JavaScript developers usually don’t like global variables, they can clash with local variables plus it’s very hard to debug such a code.
 
19
What would happen if we don't declare a variable in Javascript?
 
If you don’t declare a variable you’re essentially creating a global variable with all including downsides. This way you can’t create multiple instances of some object because each new instance will override the last one.
 
function testDeclarations(){

    primitive1 = 1;
    var primitive2 = 2;
    console.log("The first number:"+primitive1+"  <-->  The second number:"+primitive2); // will output: The first number:1  <-->  The second number:2
    primitive2 = 3;
    console.log("The first number:"+primitive1+"  <-->  The second number:"+primitive2); // will output: The first number:1  <-->  The second number:3
    
};

primitive1 = 44;

testDeclarations();

console.log("The first number:"+primitive1);  // will output: The first number:1
console.log("The second number:"+primitive2); // will output: Uncaught ReferenceError: primitive2 is not defined
 
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…