Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c0c579c

Browse files
committed
Built-in functions
1 parent c47e5b8 commit c0c579c

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

‎10-JavaScript-Functions.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,13 @@ function myFunction() {
195195
console.log(var1);
196196
}
197197

198-
console.log(x); //OUTPUT: x is not defined
198+
myFunction();
199+
console.log(var1); //OUTPUT: var1 is not defined
199200
```
200201

201202
### Lecxical Scoping
202203

203-
Lexical scoping is a key concept in JavaScript that determines how variables and functions are accessed within nested scopes, based on their position in the code.
204+
Lexical scoping is a key concept in JavaScript that determines how variables and functions are accessed within nested scopes, based on their position in the code. In simple terms, lexical scoping means that we can access the variables of outer scopes from inside our inside scope.
204205

205206
In JavaScript, every time you create a new function, it creates a new scope. When you reference a variable or function within a particular scope, JavaScript looks for it first within that scope. If it can't find it there, it looks in the next outer scope, and so on until it reaches the global scope.
206207

@@ -213,17 +214,42 @@ function myFunction() {
213214
function innerFunction() {
214215
var innerVar = "Inner Scope";
215216
console.log(outerVar); // Accessible from inner scope
217+
//OUTPUT: "Outer Scope"
216218
}
217219

218220
innerFunction();
219221
console.log(innerVar); // Not Accessible from outer scope
222+
// OUTPUT: ReferenceError: innerVar is not defined
220223
}
221224

222225
myFunction();
223226
```
224227

225228
In the example above, `myFunction` creates a new scope that has `outerVar` variable and `innerFunction` function. `innerFunction()` creates a new scope that has `innerVar` variable. When `innerFunction()` is called, it logs the value of `outerVar` which is in outer scope but we will get an error when we try to log the value of `innerVar` because it is not accessible outside the inner scope.
226229

230+
### Closures
231+
232+
Closures in JavaScript are created when a function is defined inside another function and the inner function is returned or passed as parameter to another function.
233+
234+
Closure is a function that has access to variables in its outer function's scope, even after outer function has returned. This allows inner function to continue to access and manipulate outer function's variables even after the outer function has finished executing. In simple terms, closure is a function that remembers the variables from the place it is defined, regardless of where it is executed later.
235+
236+
```js
237+
function outerScope() {
238+
var outerVar = "outer scope";
239+
240+
function innerScope() {
241+
console.log(outerVar); // OUTPUT: "outer scope"
242+
}
243+
244+
return innerScope;
245+
}
246+
247+
var inner = outerScope();
248+
inner(); // OUTPUT: "outer scope"
249+
```
250+
251+
In this example, `innerScope()` is defined inside `outsideScope()` and has access to the `outerVar` variable. When the `outerScope()` function is called and `innerScope()` function is returned,a closure is created that allows `innerScope()` function to access the `outerVar` variable even though `outerScope()` function has finished executing.
252+
227253
## IIFEs
228254

229255
`Immediately Invoked Function Expression` or IIFEs is a design pattern to protect the scope of our functions and the variables within (create a private scope).
@@ -267,3 +293,68 @@ Now, we can rewrite the code as below:
267293
```
268294

269295
Since, we do not intend to call the function again after the invocation, we do not need to give the function a name anymore.
296+
297+
## Recursion
298+
299+
Recursion is a programming pattern where a function calls itself repeatedly until certain condition is met. This is a powerful technique that allows us to solve complex problems by breaking them down into smaller, more simple parts.
300+
301+
A recursive function typically has two parts: `base case` and `recursive case`. The base case is the condition that stops the recursion and returns a value. The recursive case is where the fucntion call itself with a modified arguments.
302+
303+
Let's discuss it with a simple function that raises a number to a natural power of n.
304+
Example:
305+
306+
```
307+
power(2,3) = 8 // 2*2*2 == 8
308+
```
309+
310+
Let's implement this function with `for` loop:
311+
312+
```js
313+
function power(a, n) {
314+
let result = 1;
315+
for (let i = 0; i < n; i++) {
316+
result *= a;
317+
}
318+
return result;
319+
}
320+
321+
console.log(power(2, 3)); //OUTPUT: 8
322+
```
323+
324+
This is how we normally approach this problem.
325+
Now, let's think recursively and simplify the task.
326+
327+
```js
328+
function power(a, n) {
329+
if (n == 1) {
330+
// base case
331+
return a;
332+
} else {
333+
// recursive case
334+
return a * power(a, n - 1);
335+
}
336+
}
337+
338+
console.log(power(2, 3)); //OUTPUT: 8
339+
```
340+
341+
In this example, the base case is when `n` equals to 1 and the function returns `a`.The recursive case is when `n` is greater than 0, and the function calls itself with `a` and `n-1` as the argument.
342+
343+
When the function is called with `power(2,3)`, it will recursively call itslef with `power(2,2)`, `power(2,1)` until it reaches the base case of `power(2,0)`.
344+
345+
We can also rewrite the same code using ternary operator replacing the if-else statement.
346+
347+
```js
348+
function power(a, n) {
349+
return n == 1 ? x : x * power(a, n - 1);
350+
}
351+
```
352+
353+
## JavaScript Built-In Functions
354+
355+
### Number Methods
356+
357+
- `constructor()`: returns the function that created the object's instance. By default, this is a Number object.
358+
- `toFixed()`: formats a number with a specific number of digits to the right of decimal.
359+
- `toString()`: returns the string representation of the number's value
360+
- `valueOf()`: returns the number's value

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /