You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Implicit type coercion in javascript is the automatic conversion of value from one data type to another. It takes place when the operands of an expression are of different data types.</p>
5
36
<ul>
37
+
<li>String coercion</li>
38
+
<p>String coercion takes place while using the ‘ + ‘ operator. When a number is added to a string, the number type is always converted to the string type</p>
39
+
40
+
```javascript
41
+
var x =3;
42
+
var y ="3";
43
+
x + y // Returns "33"
44
+
```
45
+
<p>When JavaScript sees that the operands of the expression x + y are of different types ( one being a number type and the other being a string type ), it converts the number type to the string type and then performs the operation. Since after conversion, both the variables are of string type, the ‘ + ‘ operator outputs the concatenated string “33” in the first example and “24Hello” in the second example.</p>
46
+
<p> ‘ + ‘ operator when used to add two numbers, outputs a number. The same ‘ + ‘ operator when used to add two strings, outputs the concatenated string:</p>
47
+
48
+
```javascript
49
+
50
+
var name ="Riya";
51
+
var surname =" Kumari";
52
+
name + surname // Returns "Riya Kumari"
53
+
```
54
+
55
+
<p> Type coercion also takes place when using the ‘ - ‘ operator, but the difference while using ‘ - ‘ operator is that, a string is converted to a number and then subtraction takes place.</p>
56
+
57
+
```javascript
58
+
var x =3;
59
+
Var y ="3";
60
+
x - y //Returns 0 since the variable y (string type) is converted to a number type
61
+
```
62
+
</ul>
63
+
64
+
65
+
# IIFE
66
+
<p>An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined</p>
67
+
68
+
```javascript
69
+
// Regular Function.
70
+
functionGreet() {
71
+
console.log("Welcome to the Readme.md");
72
+
};
73
+
// Execution of Regular Function.
74
+
Greet();
75
+
76
+
// IIFE creation and execution.
77
+
(function() {
78
+
console.log("Welcome to Readme.md!");
79
+
})();
80
+
81
+
82
+
```
83
+
<ul>
84
+
<li>IIFEs have their own scope i.e. the variables you declare in the Function Expression will not be available outside the function.</li>
85
+
<li>Similarly to other functions IIFEs can also be named or anonymous, but even if an IIFE does have a name it is impossible to refer/invoke it.</li>
86
+
<li>IIFEs can also have parameters.</li>
87
+
</ul>
88
+
<ul>
89
+
<br>
90
+
<b>Use Cases Of IIFE</b>
91
+
<li>Avoid polluting the global namespace </li>
92
+
<li>To create closures</li>
93
+
<li>Avoid conflict of variable names between libraries and programs.</li>
94
+
</ul>
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
6
105
<!-- 1 -->
7
-
<li><b>Callback Function</b></li><p>A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.</p>
106
+
# Callback Function
107
+
<p>A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.</p>
8
108
9
109
10
110
```javascript
@@ -24,7 +124,7 @@ This repository contains a collection of daily JavaScript coding questions or co
24
124
25
125
<!-- 2-->
26
126
27
-
<li><b>Slice</b></li>
127
+
# Slice
28
128
<!-- <p>The slice() method returns a <b>shallow copy</b>(<span style="color:orange;">A shallow copy of an arrays or object is one where they both have the same reference in memory. That means that if you change the shallow copy, it may or may not change the original copy.</span>) of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.</p> -->
29
129
<p>The Javascript arr.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.</p>
30
130
@@ -51,9 +151,13 @@ console.log(a[1]);
51
151
</ul>
52
152
<br>
53
153
154
+
155
+
156
+
157
+
54
158
<!-- 3 -->
55
159
56
-
<li><b>Higher Order Functions</b></li>
160
+
# Higher Order Functions
57
161
<p>Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.</p>
58
162
59
163
```javascript
@@ -89,7 +193,7 @@ x() // Returns "Do something"
89
193
<!-- 4 -->
90
194
91
195
92
-
<li><b>Currying</b></li>
196
+
# Currying
93
197
<p>It is a technique in functional programming, that transforms the function of multiple arguments into several functions of a single argument in sequence. It is a method that takes one argument at a time and returns a new function that expects the next argument.</p>
94
198
95
199
```javascript
@@ -108,5 +212,111 @@ add(3)(4)
108
212
<li>It helps us to avoid passing the same variable multiple times</li>
109
213
<li>It is very useful in building modular and reusable code</li>
110
214
</ul>
215
+
<br>
216
+
217
+
# Call Apply Bind
218
+
<ul>
219
+
<li>Call</li>
220
+
<p>It’s a predefined method in javascript.This method invokes a method (function) by specifying the owner object.<b>call()</b> method allows an object to use the method (function) of another object.</p>
221
+
<p>This Concept is called <b>Function Borrowing</b></p>
222
+
223
+
224
+
```javascript
225
+
226
+
var person = {
227
+
age:23,
228
+
getAge:function(){
229
+
returnthis.age;
230
+
}
231
+
}
232
+
var person2 = {age:54};
233
+
person.getAge.call(person2);
234
+
// Returns 54
235
+
236
+
```
237
+
238
+
<p>call() accepts arguments:</p>
239
+
240
+
```javascript
241
+
242
+
functionsaySomething(message){
243
+
returnthis.name+" is "+ message;
244
+
}
245
+
var person4 = {name:"Riya"};
246
+
saySomething.call(person4, "awesome");
247
+
// Returns "Riya is awesome"
248
+
249
+
```
250
+
<h5>After looking at the above codes we can say that in layperson's terms, it helps you replace the value of <b>this</b> inside a function with whatever value you want.</h5>
251
+
<p><b>Syntax</b></p>
252
+
253
+
```javascript
254
+
call(objectInstance)
255
+
call(objectInstance, arg1, /* …, */ argN)
256
+
```
257
+
258
+
<li>Apply</li>
259
+
<p>Apply is very similar to the call function. The only difference is that call() method takes arguments separately whereas, apply() method takes arguments as an <b>array</b>.</p>
260
+
261
+
262
+
```javascript
263
+
functionsaySomething(message){
264
+
returnthis.name+" is "+ message;
265
+
}
266
+
var person4 = {name:"Riya"};
267
+
saySomething.apply(person4, ["awesome"]);
268
+
```
269
+
270
+
<p>The best part about apply is we don’t need to take care of the number of arguments that are passed to the invoking function. Because of its dynamic and versatile nature, we can use it in complicated situations.</p>
271
+
272
+
<li>Bind</li>
273
+
<p>This method returns a new function, where the value of “this” keyword will be bound to the owner object, which is provided as a parameter.</p>
274
+
275
+
```javascript
276
+
functionsaySomething(message){
277
+
returnthis.name+" is "+ message;
278
+
}
279
+
var person4 = {name:"Riya"};
280
+
let Greet =saySomething.bind(person4, "awesome");
281
+
console.log(Greet());
282
+
```
283
+
284
+
285
+
<p>The only difference between the call and bind is that it gives you copy of the function which can be invoked later rather than directly invoking it .</p>
286
+
</ul>
287
+
288
+
# Optional chaining
289
+
<p>The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.
290
+
The <b>optional chaining (?.)</b> operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.</p>
291
+
292
+
# Inheritance
293
+
294
+
```javascript
295
+
296
+
classsurname {
297
+
constructor() {
298
+
console.log("kumari 💫");
299
+
}
300
+
}
301
+
302
+
classNameextendssurname {
303
+
constructor() {
304
+
console.log("Riya");
305
+
super();
306
+
}
307
+
}
308
+
309
+
constuser=newName();
310
+
```
311
+
312
+
Explanation :
313
+
314
+
```javascript
315
+
constuser=newName();
316
+
```
317
+
318
+
<p>
319
+
This line creates an instance of the Name class, which triggers the constructor of the Name class. Inside the constructor, "Riya" is logged to the console, and then the super() method is called, which triggers the constructor of the surname class. In the surname constructor, "kumari 💫" is logged to the console.
0 commit comments