1. What are the types of variable in JavaScript?
Ans: Two types of variables
- Local variable
- Global variable
2. What is the use of typeof in JavaScript?
Ans: It is used to find the datatype of variable.
3. How to create Objects in JavaScript?
Ans: Three ways to create Objects in JavaScript.
- By object literal.
- By creating instance of object.
- By object constructor.
4. How to create an Array in JavaScript?
Ans: Three ways to create Array in JavaScript.
- By array literal.
- By creating instance of array.
- By using an Array constructor.
5. What are the pop-up boxes available in JavaScript?
Ans: Three kinds of Pop-up boxes available in JavaScript.
- Alert box
- Confirm box
- Prompt box
6. How to set the cursor to wait in JavaScript?
Ans: The cursor can be set to wait in JavaScript by using property “cursor”.
Ex: window.document.body.style.cursor=”wait”;
7. What is the use of debugger Keyword in JavaScript?
Ans: JavaScript debugger keyword sets the breakpoint through the code itself. The debugger stops the execution of the program at the position it is applied.
8. What do you understand by Hoisting in JavaScript?
Ans: Hoisting is the default behavior of JavaScript, where all the variable and function declarations are moved on top.
They are moved on the top of scope. The scope can be both local and global.
9. What is Anonymous Functions?
Ans: Function without a name is known as Anonymous function.
In JavaScript, we can pass a function as an argument, that’s where the anonymous functions will be used.
10. What is Function Expressions?
Ans: If a function can be referred with a variable, then it is known as Function Expressions.
11. What is First Class Functions?
Ans: If a function can be passed as an argument, referred with a variable and returned from another function then we call those functions as first class functions.
12. Difference between Normal and Arrow function in JavaScript?
Ans: Normal function return value and is declared using function keyword whereas Arrow function does not return any value and it can be declared without using function keyword.
13. What is Closures?
Ans: A Closure is a scope that gets formed when a function(inner) is created inside another function.
The reason to create inner functions is to create private functions.
14. Is it possible that what we create an object can’t be modified?
Ans: Yes it is possible that whatever object we create can’t be modified.
Ex: var v1= Object.freeze({ });
15. What is Scopes and it’s type?
Ans: Scopes determines the accessibility(visibility) of variables.
There are several types of scopes:
- Local Scope
- Global Scope
- Functional Scope
- Block Scope
16. What is the Scope of var, let and const?
Ans: Var is a functional scope whereas let and const are block scope.
17. What is Variable shadowing?
Ans: Variable shadowing occurs when a inner scope declares a variable name which is same as of outer scope variable.
If we try to shadow let variable by var variable it is known as illegal shadowing and it give us the error that variable is already define. But if we try to shadow var variable by let variable it will work.
18. What is Arrow Function?
Ans: Function without a name and are not bounded by an identifier. Arrow function do not return any value and can be declared without the function keyword. It is also known as lambda function.
19. What is Callback Function?
Ans: Passing function definition as a parameter/argument to another function is called as Callback function.
Syntax: Function x() {
}
function y(x){
}
y(x);
20. What is promises?
Ans: A Promises is an object that represent the completion or failure of an asynchronous operation.
Asynchronous operation: Task that will be executed after certain period of time.
Syntax: Const Promise= new Promise(function(resolve, reject){
resolve (value);
reject (value);
});
Promise.then(function(response) {
//success dependent statements
}).catch(function(error){
//failure dependent statements });
21. What is Shift and unshift in JavaScript?
Ans: Shift function is used to remove first element from array whereas Unshift function is used to add element in the first position of array.
Ex: Shift
var a=[1,2,3,4,5];
a.Shift();
console.log(a);
o/p – [2,3,4,5]
UnShift
var b=[2,3,4,5];
b.unShift(7);
console.log(b);
o/p – [7,2,3,4,5]
22. What is Call, Apply and Bind Function?
Ans: Call: It is used to call a function of other object by substituting another object.
Ex: var obj1= { name: “aman” , addr: “HYD”,
getdetails: function () {
return this.name + “\t” + this.addr } }
var obj2={ name: “kishan” , addr: “Pune” }
console.log(obj1.getdetails.call(obj2));
Apply: Same as call function but if we have any arguments to be passed, then apply function will automatically spread the array of values.
Ex: var obj1= { name: “aman” , addr: “HYD”,
getdetails: function (a, b, c) {
return this.name + “\t” + this.addr + “\t” + a + “\t” + b + “\t” + c } }
var arr1=[20,56,34]
var obj2={ name: “kishan” , addr: “Pune” }
console.log(obj1.getdetails.apply(obj2.arr1));
Bind: It is exactly same as call function but only difference is instead of directly calling function it bind the new object with existing object and return the copy of method.
Ex: var obj1= { name: “aman” , addr: “HYD”,
getdetails: function (ocup) {
return this.name + “\t” + this.addr + “\t” + ocup } }
var obj2={ name: “kishan” , addr: “Pune” }
let d=obj1.getdetails.bind(obj2,”Btech”);
console.log(d());
23. What is Call by Value and Call by Reference?
Ans: Call by Value is always used with primitive datatype, in which original variable is not modified on change in other variables.
Ex: let x=5;
let y=x;
x=3;
console.log(x);
console.log(y);
o/p – 3
5
Call by Reference is used with non-primitive datatype, in which original variable get modified on changes in other variables.
Ex: var obj1= {name: “Ravi”};
var obj2=obj1;
obj2.name=”Akash”;
console.log(obj1);
console.log(obj2);
o/p – { name: “Akash”}
{ name: “Akash”}
24. What is Rest Operator?
Ans: It is used to store rest of the parameters passed to function in an array.
Ex: function s1(…nums){
let total=0;
for(const nm of nums){
total +=nm; }
return total; }
const res=console.log(s1(1,2,3,4,5));
// This returns 15(1+2+3+4+5)
25. What are the types of Coercion in JavaScript?
Ans: There are 2 types of Coercion in JavaScript.
Explicit Coercion: It occurs when we converts a value from one type to another using function or operator.
Ex: var x=42;
var z=String(x);
console.log(z);
Implicit Coercion: It occurs when JavaScript automatically converts a value from one type to another during an operation.
Ex: var str= “5”;
var x=10;
var z=str + x;
console.log(z);