https://career.guru99.com/top-85-javascript-interview-questions/
Problem
1 2 3 4 5 6 7 8 9 10 11 |
console.log('1') // 1) logs 1 // 2) run function afterTwoSeconds() after 2 seconds setTimeout(function afterTwoSeconds() { console.log('2') }, 2000) // 3) logs 3 console.log('3') // 2 seconds later, logs 2. |
Answer
This will actually log “1 3 2”, since the “2” is on a setTimeout which will only execute, by this example, after two seconds. Your application does not hang waiting for the two seconds to finish. Instead it keeps executing the rest of the code and when the timeout is finished it returns to afterTwoSeconds.
Problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function foo1() { return { bar: "hello" }; } function foo2() { return // automatically inserts a semi-colon. Thus returning nothing { bar: "hello" }; } |
Answer
Not only is this surprising, but what makes this particularly gnarly is that foo2() returns undefined without any error being thrown.
The reason for this has to do with the fact that semicolons are technically optional in JavaScript (although omitting them is generally really bad form). As a result, when the line containing the return statement (with nothing else on the line) is encountered in foo2(), a semicolon is automatically inserted immediately after the return statement.
No error is thrown since the remainder of the code is perfectly valid, even though it doesn’t ever get invoked or do anything (it is simply an unused code block that defines a property bar which is equal to the string “hello”).
This behavior also argues for following the convention of placing an opening curly brace at the end of a line in JavaScript, rather than on the beginning of a new line. As shown here, this becomes more than just a stylistic preference in JavaScript.
Problem
1 2 3 4 5 6 |
(function(){ var a = b = 3; })(); console.log("a defined? " + (typeof a !== 'undefined')); console.log("b defined? " + (typeof b !== 'undefined')); |
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
(function(){ b = 3; // ends up being a global variable (since it is not preceded by the var keyword) console.log(b); var a = b; console.log(a); })(); console.log(b); // defined. b is global console.log(typeof b); // since b does not have var in front, it is designated as a global. //console.log(a); // a is NOT defined at this scope. It is only visible in the function. console.log("a defined? " + (typeof a !== 'undefined')); // a is undefined because it is local only to the function console.log("b defined? " + (typeof b !== 'undefined')); // b is defined because it is a global |
Problem
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var myObject = { foo: "bar", func: function() { var self = this; console.log("outer func: this.foo = " + this.foo); console.log("outer func: self.foo = " + self.foo); (function() { console.log("inner func: this.foo = " + this.foo); console.log("inner func: self.foo = " + self.foo); }()); } }; myObject.func(); |
Solution
In the outer function, both this and self refer to myObject and therefore both can properly reference and access foo.
In the inner function, though, this no longer refers to myObject. As a result, this.foo is undefined in the inner function, whereas the reference to the local variable self remains in scope and is accessible there.
1 2 3 4 |
outer func: this.foo = bar outer func: self.foo = bar inner func: this.foo = undefined inner func: self.foo = bar |
Problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var myApp = {} myApp.id = 0; myApp.next = function() { return this.id++; } myApp.reset = function() { this.id = 0; } myApp.next(); //0 myApp.next(); //1 var getNextId = myApp.next; getNextId(); //NaN whoops! |
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
var myApp = {} // declare a literal object myApp.id = 0; // add an id property and initialize it to 0 // add a property 'next'. It references a function object myApp.next = function() { return this.id++; } // when next is called, the this references the literal object myApp. console.log(myApp.next()); // 0 console.log(myApp.next()); // 1 var myApp2 = {} // let's make another literal object. This time, no property id is added. // add property next2, and assign it to the next function //so the two literal objects share the same function. myApp2.next2 = myApp.next; // When myApp2 literal object executes the "next" function, we get a NaN because 'id' does not exist // for myApp 2. console.log(myApp2.next2()); // getNextId is simply a reference to the next function // its 'this' references global var getNextId = myApp.next; //console.log(getNextId()); //Nan // however, if we define id, then it will be ok id = 6680; console.log(getNextId()); // 6680 console.log(getNextId()); // 6681 console.log(getNextId()); // 6682 |
What is JavaScript?
JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object Oriented Programming language.
What are JavaScript Data Types?
Following are the JavaScript Data types:
Number
String
Boolean
Function
Object
Undefined
What is the use of isNaN function?
isNan function returns true if the argument is not a number otherwise it is false.
What is negative infinity?
Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.
Is it possible to break JavaScript Code into several lines?
Breaking within a string statement can be done by the use of a backslash, ‘\’, at the end of the first line
Example:
1 |
document.write("This is \a program"); |
What are undeclared and undefined variables?
Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.
1 |
console.log(a) // runtime error, a was never declared anywhere in scope. |
Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.
Uninitialized variables
1 2 |
let a; console.log(a) // undefined |
> var noValueYet;
> console.log(noValueYet);
undefined
missing parameters
1 2 3 4 5 6 |
function add(a, b) { console.log(a) // 2 console.log(b) // undefined } add(2) // missing 2nd parameter |
> function foo(x) { console.log(x) }
> foo()
undefined
unknown variables of objects
> var obj = {};
> console.log(obj.unknownProperty)
undefined
null: used by programmers to indicate “no value”, e.g. as a parameter to a function.
Examining a variable:
console.log(typeof unknownVariable === “undefined”); // true
var foo;
console.log(typeof foo === “undefined”); // true
console.log(foo === undefined); // true
var bar = null;
console.log(bar === null); // true
Write the code for adding new elements dynamically?
1 2 3 |
var h = document.createElement("H1") // Create a <h1> element var t = document.createTextNode("Hello World"); // Create a text node h.appendChild(t); // Append the text to <h1> |