JavaScript Important Interview Questions
Level - Basic
JavaScript (JS) is a light weight client side programming language. By client side we mean JS is executed in client side (i.e. Browser) and not in server. JS is used to make web pages client side dynamic like showing dynamic content in web pages, showing error/alert messages to user based on their input in forms (form validation), animating elements of web pages, dynamically changing style and so on. You can say JS is like the brain of a web page.
Yes, JavaScript is case sensitive. For example, user & User are two different variables in JavaScript.
The window.location object provides current page address (URL) and it can be used to redirect the browser to a new page.
The window.location.href returns the href (URL) of the current page.
The current history.back() method redirects back to previous page (same as clicking back in the browser).
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller.
function getMessage() {
let message = 'Hello!';
return message;
}
function divide(x,y) {
if(y==0) return; // Stop execution here if y=0
let result = x/y;
return result;
}
Yes, we can call two functions on a button click. Define two functions and call the first function inside second function. On button click call the second function.
func1(){
// body
}
func2(){
// body
func1();
}
<button onclick="func2()">Call</button>
In JavaScript, as per latest ECMAScript standard there are nine different Data Types which can be grouped into three broad categories –
Primitive or Primary - undefined, Boolean, Number, String, BigInt and Symbol
Non-data Structure or Composite – Object and Function
Special – null
For more details read this article:
Data Types in JavaScript
The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal.
false == '0'; // true
false === '0'; // false
An IIFE (Immediately Invoked Function Expression) pronounced as iify is a JavaScript function that runs as soon as it is defined. In other words, the function gets executed automatically as soon as the page loads. There is no need to call this function anywhere in the script. JavaScript engine directly interprets the function.
For more details read this article:
What is IIFE in JavaScript
The output would be 57. In the expression "7" is string. JavaScript will sum up the numbers 3+2=5 and concat it with "7". So 3+2 = 5 and 5+"7" = 57
In JavaScript, objects can be created as follows:
let obj = {
name:"Lokesh",
age:"30",
profession: "Developer"
}
Object consist of key/value pairs. The object obj in the above example has three keys - name, age and profession.