-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSInterviewQuestionPractice
More file actions
151 lines (136 loc) · 6.83 KB
/
JSInterviewQuestionPractice
File metadata and controls
151 lines (136 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
Q:What is difference between var and let?
Ans:
1. Let is ES15. Var is there since beginning
2. Let has block scope. Var has function scope.
3. Variable created by VAR gets hoisted but by LET it does not.
=================================================================================================
Q: What is difference between == and === ?
Ans:
1. == checks only value. === checks value and type.
Better answer: == converts type of RHS to LHS and then compare value. === doesnt do that.
=================================================================================================
Q:What is difference between const and let?
1. You can;t change const value once assigned. In let you can assign as many times as you want.
const c=1;
c=2; // error
const c1 = [1,2];
c1.push(3); // gives [1,2,3] .. you can modify objects but not re-assign.
==================================================================================================
what is difference between undefined and null?
1. They both represent empty value. If you declared value but not assigned it takes undefined. null is used to make value clear.
2. typeof(undefined) = undefined. typeof(null) = object.
==================================================================================================
what is difference between function declaration and funcion expression?
function foo(){
console.log("function declaration");
}
let foo = function(){
console.log("function expression"); // used to pass function as parameter
}
==================================================================================================
What is arrow functions?
1. Arrow function makes code concise without use of function and return keyword.
var multiplyES5 = function(x, y) {
return x * y;
};
// ES6
const multiplyES6 = (x, y) => { x * y };
2) In code with multiple nested functions, it can be difficult to keep track of and remember to bind the correct this context.
In ES5, you can use workarounds like the .bind method (which is slow) or creating a closure using var self = this;.
Because arrow functions allow you to retain the scope of the caller inside the function, you don’t need to create self = this
closures or use bind.
=====================================================================================================
what is protype inheritance?
Every object in JS has property prototype. When you create an object from other object you inherit all the property from the parent.
let car = functio(modal)
{
this.modal = modal;
}
car.protype.getModal = function(){ return this.modal;}
let honda = new car('Honda');
let hundai = new car('hundai');
console.log(honda.getModal()); // prints honda
console.log(hundai.getModal()); // prints hundai
===================================================================================================================================
what is promises? Why we should use them ?
Used in Async programming. When you are waiting something to happen and then want to call some another callback.
Nested callbacks are hard to erad hard to debug and can lead to callback hell.
var p1 = new Prmoise(function(resolve, reject){
});
p1.then()
===================================================================================================================================
what is clousure? whey we should use them?
when a function return a function and it holds the environment of the function and that is called the closure
let obj = function(){
let i =0;
return {
setI(k){i=k;}.
getI(){ return i}
}
}
===================================================================================================================================
what happens when SetTimeout(fun(){}, 0) is executed?
0 doesnt mean that it will execute right way. It will be put in event loop and it will exceute when all sync call gets executed.
===================================================================================================================================
What is variable and function hoisting
JavaScript interpreter "looks ahead" to find all the variable declarations and "hoists" them to the top of the function.
var declaredLater;
console.log(declaredLater); // Outputs: undefined
declaredLater = "Now it's defined!";
console.log(declaredLater); // Outputs: "Now it's defined!"
So that covers variable hoisting, but what about function hoisting? Despite both being called "hoisting,"
the behavior is actually quite different. Unlike variables, a function declaration doesn't just hoist the function's name. It also hoists the actual function definition.
isItHoisted(); // Outputs: Yes!
function isItHoisted() {
console.log("Yes!");
}
===================================================================================================================================
What is close method in Javascript do?
if its called on window onject it closes the window and if its called on the document object it closes the document.
======================================================================================================================================
Whats the output of
console.log([]+[]) or console.log({} + [])// outputs nothing
explanation: empty array[]/empty obejct{} is converted to empty string and empty string + empty string = empty string
=======================================================================================================================================
whats the output of
function a(){
return 'hello';
}
const sentence = a 'hi';
console.log(sentence);
Explanation: a 'hi' is considered as TAGGED TEMPLATE and will be same as a('hi'); hence it will print and hi will be ignored as a function doesnt take
any argument
===========================================================================================================================================
take any web page and make its content editable.
Answer:
<div contenteditable="true"> Hello
</div> /// makes it editable
to make the page editable
document.body.contentediatble = true;// makes it editable
============================================================================================================================
whats the output of
function y(){
console.log(this.length):
}
var x = {
length:5,
method:function(y){
arguments[0]();
}
}
x.method(y,1); // ot outputs 2 .. beca
=================================================================================================================================
const x = 'constructor';
cosole.log(x[x](01));
x[constructor] = string
string(01) //outputs 1;
=====================================================================================================================================
whats the output
console.log(0.1+0.2); // outputs 0.30000000000004 becoz numbers are treated as floating point precision
to solve this issue
if()
=============================
cosnole.log(('hi').__proto.__proto)
'hi' - is string
string.__proto gives Object
Object.__proto gives