Top 15 JavaScript Interview Questions
Top 15 JavaScript Interview Questions
Interview Questions
for Web Development
Roles
1. What are the advantages of using JavaScript?
Answer: Lightweight: JavaScript can be executed within the user’s browser
without having to communicate with the server, saving on bandwidth.
Versatile: JavaScript supports multiple programming paradigms—object-
oriented, imperative, and functional programming and can be used on
both front-end and server-side technologies.
Sleek Interactivity: Because tasks can be completed within the browser
without communicating with the server, JavaScript can create a smooth
"desktop-like" experience for the end user.
Rich Interfaces: From drag-and-drop blocks to stylized sliders, there are
numerous ways that JavaScript can be used to enhance a website’s
UI/UX.
Prototypal Inheritance: Objects can inherit from other objects, which
makes JavaScript so simple, powerful, and great for dynamic applications.
3. What will the code below output to the console and why?
JavaScript
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();
Answer: The above code will output the following to the console:
JavaScript
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.
JavaScript
(a) What gets logged to the console when the user clicks on “Button 4” and
why?
(b) Provide one or more alternate implementations that will work as
expected.
Answer: (a) No matter what button the user clicks the number 5 will always be
logged to the console. This is because, at the point that the onclick method is
invoked (for any of the buttons), the for loop has already completed and the
variable i already has a value of 5. (Bonus points for the interviewee if they
know enough to talk about how execution contexts, variable objects,
activation objects, and the internal “scope” property contribute to the closure
behavior.)
(b) The key to making this work is to capture the value of i at each pass
through the for loop by passing it into a newly created function object. Here
are four possible ways to accomplish this:
JavaScript
JavaScript
Or, we could replace the for loop with a call to the array object’s native
forEach method:
JavaScript
JavaScript
Example 2:
hoistedVariable = 5;
console.log(hoistedVariable); // outputs 5 though the
variable is declared after it is initialized
var hoistedVariable;
11. Explain briefly how you can submit a form using JavaScript?
Answer: In JavaScript, you can use an on click event for submitting a form,
i.e., form.submit() method.
You can perform a submit action by using submit button, By clicking on
Hyperlinks.
For submitting a form using JavaScript use document.form[0].submit();
For Example:
JavaScript
document.form[0].submit();
JavaScript
document.getElementById("myText").style.fontSize = "20";
or,
JavaScript
JavaScript
In HTML:
1
<element onblur=”HTML”>
In JavaScript:
1
object.onblur= function(){JavaScript};
19. How can you convert the string of any base to integer in
JavaScript?
Answer: The parseInt() function is used to convert numbers between different
bases. It takes the string to be converted as its first parameter, and the
second parameter is the base of the given string.
For example-
1
parseInt("4F", 16)
20. What are the two sides of a JavaScript, and how would you
differentiate between them?
Answer: JavaScript has two sides, namely the server-side JavaScript and the
client-side JavaScript. A developer is called a full stack JavaScript developer if
he can develop both sides.
The Client side JavaScript refers to the part of the code that runs on the client
machine. The client-side JavaScript incorporates simple, predefined
programming language and objects that are necessary for successfully running
the codes in a browser. The client-side JavaScript is interpreted by the
browsers at runtime, and it is directly embedded into the HTML pages.
The Server side JavaScript refers to the part of the code that is run on the main
server that is providing the web pages. Deployment of the server-side
JavaScript is performed after complete compilation. Node.js – in simple words
– is server-side JavaScript. The developer can use Node.js to provide server-
side functionality to your JavaScript-based projects.