JavaScript Interview Questions
This page consists of JavaScript interview questions and answers.
Global variables are the variables that are available throughout the code.
If we create a variable without the var
keyword then it becomes a global variable.
this
keyword in JavaScript?The this
keyword is used to refer to the object from where it is called.
"use strict";
in JavaScript?The "use strict";
in JavaScript is used to execute the code in strict mode.
So, for example in strict mode we can't use undeclared variables.
"use strict";
is a literal expression and not a statement and is ignored by older JavaScript versions.
setTimeout
in JavaScript?The setTimeout
function executes a function after a given delay.
In the following example we will get "Hello World" message in the browser console after 5000ms (milliseconds) delay.
setTimeout(function() {
console.log('Hello World');
}, 5000)
setInterval
in JavaScript?The setInterval
function is used to execute a function repeatedly after a given delay. The execution stops only when we stop the timer using the clearInterval
function.
In the following example we will get "Hello World" after every 3 seconds (3000ms) for 5 times.
var count = 0;
var t = setInterval(function() {
console.log('Hello World');
count++;
if (count === 5) {
clearInterval(t);
console.log('End');
}
}, 3000);
View state is specific to a page in a session. Whereas, Session state is specific to a user or browser session and is accessible from all pages.
Following are the loop structure available in JavaScript.
var str = 'Hello World';
for (var i = 0, len = str.length; i < len; i++) {
console.log(str[i]);
}
console.log(typeof typeof null);
The output is "string"
.
Explanation:
The typeof null
will give us "object"
. This is in double quotes so, it is a string.
Next, the typeof "object"
will give us "string"
.
Hence, the final answer is "string".
Void(0)
is used to prevent a page from refreshing when used with hyperlinks.
In the following example we have a hyperlink which when clicked will not refresh the page or jump to the top of the page.
<a href="javascript:void(0);" ondblclick="alert('Hello World')">Double click me</a>
ADVERTISEMENT