• Tutorials Logic, IN
  • +91 8092939553
  • info@tutorialslogic.com

JavaScript Interview Questions

1. Explain closures in JavaScript ?

JavaScript variables scopes can be a local or global. Global variables can be made local with the help of closures, because closure gives us access to an outer function’s scope from an inner function, even after the outer function has returned. In JavaScript, closures are created every time when we create function. The closure has three scope chains: it has access to its own variables, it has access to the outer function’s variables, and it has access to the global variables.

To use a closure, just define a function inside another function and expose it. To expose a function, return it or pass it to another function.

										    
											function greeting(name) {
												var message = "Hello";
												// This inner function has access to the outer function's variables, including the parameter
												function showGreeting() {
													return message + " " + name;
												}
												return showGreeting();
											}
											var greet = greeting("Tutorials Logic");
											console.log(greet); // This will give output as "Hello Tutorials Logic"
											
										

2. What is the difference between ViewState and SessionState ?

A ViewState is specific to a page in a session, where as a SessionState is specific to user specific data on the server side that can be accessed across all pages in the web application.

3. Does JavaScript support automatic type conversion ?

Yes, JavaScript does support automatic type conversion. It is the most common way of type conversion used by JavaScript developers or programmers.

4. What is the difference between "==" and "===" ?

"==" checks only for equality in value where as "===" checks for type as well as equality in value.

										    
											var a = 2;
											var b = '2';
											a == b; // True, because "==" will not check for the type.
											a === b; // False, because b is of string type.