Skip to content

Commit bfdfd13

Browse files
committed
Update examples and formatting
1 parent cd71c2d commit bfdfd13

File tree

4 files changed

+227
-82
lines changed

4 files changed

+227
-82
lines changed

README.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
## How to run examples
66
```cmd
77
npm install
8-
npx babel-node es2020/bigint // Try other examples too
8+
npx babel-node es2020\bigint.js # Try other examples too
99
```
1010

1111
# What is ECMAScript?
1212

13-
**ECMAScript** is the scripting language which acts as the basis of JavaScript. ECMAScript standardized by the ECMA International standards organization in the ECMA-262 and ECMA-402 specifications.
13+
**ECMAScript** is the scripting language which acts as the basis of JavaScript. ECMAScript is standardized by the ECMA International standards organization in the ECMA-262 and ECMA-402 specifications.
1414
Each proposal for an ECMAScript feature goes through the following maturity stages:
1515

1616
1. Stage 0: Strawman;
@@ -1151,9 +1151,9 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
11511151
console.log(factorial(1000));
11521152
console.log(factorial(10000));
11531153
```
1154-
1154+
11551155
**[⬆ Back to Top](#table-of-contents)**
1156-
1156+
11571157
24. ### Array find methods
11581158
ES6 introduced few array methods and two of them are `Array.find()` and `Array.findIndex()`.
11591159
@@ -1219,7 +1219,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
12191219
console.log(numbers.includes(NaN)); // true
12201220
console.log(numbers.includes(undefined)); // true
12211221
```
1222-
1222+
12231223
**[⬆ Back to Top](#table-of-contents)**
12241224
12251225
2. ### Exponentiation Operator
@@ -1266,9 +1266,9 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
12661266
}
12671267
logger();
12681268
```
1269-
1269+
12701270
**[⬆ Back to Top](#table-of-contents)**
1271-
1271+
12721272
2. ### Object values
12731273
Similar to Object.keys which iterate over JavaScript object’s keys, Object.values will do the same thing on values. i.e, The Object.values() method is introduced to returns an array of a given object's own enumerable property values in the same order as `for...in` loop.
12741274
@@ -1286,9 +1286,9 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
12861286
console.log(Object.values(['India', 'Singapore'])); // ['India', 'Singapore']
12871287
console.log(Object.values('India')); // ['I', 'n', 'd', 'i', 'a']
12881288
```
1289-
1289+
12901290
**[⬆ Back to Top](#table-of-contents)**
1291-
1291+
12921292
3. ### Object entries
12931293
The `Object.entries()` method is introduced to returns an array of a given object's own enumerable string-keyed property [key, value] pairsin the same order as `for...in` loop.
12941294
```js
@@ -1308,9 +1308,9 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
13081308

13091309
console.log(Object.entries(100)); // [], an empty array for any primitive type because it won't have any own properties
13101310
```
1311-
1311+
13121312
**[⬆ Back to Top](#table-of-contents)**
1313-
1313+
13141314
4. ### Object property descriptors
13151315
13161316
Property descriptors describe the attributes of a property. The `Object.getOwnPropertyDescriptors()` method returns all own property descriptors of a given object.
@@ -1334,7 +1334,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
13341334
const descriptors = Object.getOwnPropertyDescriptors(profile);
13351335
console.log(descriptors); // {age: {configurable: true, enumerable: true, writable: true }}
13361336
```
1337-
1337+
13381338
**[⬆ Back to Top](#table-of-contents)**
13391339
13401340
5. ### String padding
@@ -1363,7 +1363,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
13631363
console.log((label1 + ': ').padEnd(20, ' ') + value1); // Name: John
13641364
console.log(label2 + ": " + value2); // Phone Number: (222)-333-3456
13651365
```
1366-
1366+
13671367
**[⬆ Back to Top](#table-of-contents)**
13681368
13691369
6. ### Shared memory and atomics
@@ -1511,7 +1511,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
15111511
const myObject = { a: 1, b: 2, c: 3, d:4 };
15121512
const myNewObject = { ...myObject, e: 5 }; // { a: 1, b: 2, c: 3, d: 4, e: 5 }
15131513
```
1514-
1514+
15151515
**[⬆ Back to Top](#table-of-contents)**
15161516
15171517
3. ### Promise finally
@@ -1580,7 +1580,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
15801580

15811581
console.log(numberArray1.flatMap(value => [value * 10])); // [10, 20, 30, 40, 50]
15821582
```
1583-
1583+
15841584
**[⬆ Back to Top](#table-of-contents)**
15851585
15861586
2. ### Object fromEntries
@@ -1620,7 +1620,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
16201620

16211621
Object.fromEntries(searchParams); // => {param1: "foo", param2: "baz"}
16221622
```
1623-
1623+
16241624
**[⬆ Back to Top](#table-of-contents)**
16251625
16261626
3. ### String trimStart and trimEnd
@@ -1638,7 +1638,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
16381638
console.log(messageTwo.trimStart()); //Hello World!!
16391639
console.log(messageTwo.trimEnd()); // Hello World!!
16401640
```
1641-
1641+
16421642
**[⬆ Back to Top](#table-of-contents)**
16431643
16441644
4. ### Symbol description
@@ -1658,7 +1658,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
16581658

16591659
console.log(Symbol.iterator.description); // "Symbol.iterator"
16601660
```
1661-
1661+
16621662
**[⬆ Back to Top](#table-of-contents)**
16631663
16641664
5. ### Optional catch binding
@@ -1688,9 +1688,9 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
16881688
}
16891689
} catch (unused) {}
16901690
```
1691-
1691+
16921692
**[⬆ Back to Top](#table-of-contents)**
1693-
1693+
16941694
6. ### JSON Improvements
16951695
16961696
JSON is used as a lightweight format for data interchange(to read and parse). The usage of JSON has been improved as part of ECMAScript specification. Basically there are 2 important changes related to JSON.
@@ -1722,7 +1722,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
17221722
```js
17231723
console.log(JSON.stringify("\uD800")); // '"\ud800"'
17241724
```
1725-
1725+
17261726
**[⬆ Back to Top](#table-of-contents)**
17271727
17281728
7. ### Array Stable Sort
@@ -1765,7 +1765,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
17651765
// console.log(`Hello, ${msg}`);
17661766
// }
17671767
```
1768-
1768+
17691769
**[⬆ Back to Top](#table-of-contents)**
17701770
17711771
9. ### Private Class Variables
@@ -1888,7 +1888,7 @@ Most of these features already supported by some browsers and try out with babel
18881888
console.log(vehicle.car.speed ?? 90); // 0(zero is valid case for speed)
18891889
```
18901890
In a short note, nullish operator returns a non-nullish value and || operator returns truthy values.
1891-
1891+
18921892
**[⬆ Back to Top](#table-of-contents)**
18931893
18941894
4. ### String matchAll
@@ -1907,7 +1907,7 @@ Most of these features already supported by some browsers and try out with babel
19071907
["test1", "e", "st1", "1", index: 0, input: "test1test2", groups: undefined]
19081908
["test2", "e", "st2", "2", index: 5, input: "test1test2", groups: undefined]
19091909
```
1910-
1910+
19111911
**[⬆ Back to Top](#table-of-contents)**
19121912
19131913
5. ### Optional chaining
@@ -1938,7 +1938,7 @@ Most of these features already supported by some browsers and try out with babel
19381938
console.log(vehicle.car?.name ?? "Unknown"); // Unknown
19391939
console.log(vehicle.car?.speed ?? 90); // 90
19401940
```
1941-
1941+
19421942
**[⬆ Back to Top](#table-of-contents)**
19431943
19441944
6. ### Promise.allSettled
@@ -1957,7 +1957,7 @@ Most of these features already supported by some browsers and try out with babel
19571957
As per the output, each outcome object returns `status` field which denotes either "fulfilled"(value present) or "rejected"(reason present)
19581958
19591959
**[⬆ Back to Top](#table-of-contents)**
1960-
1960+
19611961
7. ### globalThis
19621962
Prior to ES2020, you need to write different syntax in different JavaScript environments(cross-platforms) just to access the global object. It is really a hard time for developers because you need to use `window, self, or frames` on the browser side, `global` on the nodejs, `self` on the web workers side.
19631963
@@ -2071,7 +2071,7 @@ Most of these features already supported by some browsers and try out with babel
20712071
WeakRef provides two new pieces of functionality
20722072
1. creating weak references to objects with the WeakRef class
20732073
2. running user-defined finalizers after objects are garbage-collected, with the FinalizationRegistry class
2074-
2074+
20752075
**WeakRef:**
20762076
weak reference is a reference to an object that doesn’t prevent garbage collection if it is the only reference to the object in the memory.It’s useful when we don’t want to keep the object in memory forever(e.g, WebSocket). The main use of weak references is to implement caches or mappings to large objects for which you don't need to keep it in memory for rarely used objects.
20772077
@@ -2132,7 +2132,7 @@ Most of these features already supported by some browsers and try out with babel
21322132
21332133
5. ### Logical Operators
21342134
Logical assignment operators combines the logical operations(&&, || or ??) with assignment. They are quite useful for assigning default values to variables.
2135-
2135+
21362136
**&&=:**
21372137
21382138
The `&&=` operator performs the assignment only when the left operand is truthy.
@@ -2154,7 +2154,7 @@ Most of these features already supported by some browsers and try out with babel
21542154
```
21552155
21562156
**||=:**
2157-
2157+
21582158
The ||= operator performs the assignment only when the left operand is falsy.
21592159
```javascript
21602160
let x = 0;
@@ -2174,7 +2174,7 @@ Most of these features already supported by some browsers and try out with babel
21742174
```
21752175
21762176
**??=:**
2177-
2177+
21782178
The ??= operator performs the assignment only when the left operand is null or undefined.
21792179
```javascript
21802180
let x;
@@ -2259,7 +2259,7 @@ Most of these features already supported by some browsers and try out with babel
22592259
22602260
```javascript
22612261
class Employee {
2262-
2262+
22632263
constructor() {
22642264
this.name = "John"; //public
22652265
this._age=35; //private
@@ -2289,7 +2289,7 @@ Most of these features already supported by some browsers and try out with babel
22892289
#getAge() {
22902290
return #age
22912291
}
2292-
2292+
22932293
}
22942294

22952295
const employee = new Employee();
@@ -2371,9 +2371,9 @@ Most of these features already supported by some browsers and try out with babel
23712371
**[⬆ Back to Top](#table-of-contents)**
23722372
23732373
5. ### hasOwn
2374-
2374+
23752375
The new `Object.hasOwn()` method is a replacement or improved version of `Object.prototype.hasOwnProperty`. It is a static method that returns true if the specified object has the indicated property as its own property. If the property is inherited, or does not exist, the method returns false.
2376-
2376+
23772377
It's not only more concise and readable but also overcomes the below limitations of `hasOwnProperty`.
23782378
23792379
1. **When `hasOwnProperty`overwritten:**
@@ -2407,7 +2407,7 @@ Most of these features already supported by some browsers and try out with babel
24072407
2. **Create an object with create(null) function:**
24082408
24092409
If you create new object with help of create(null) function, then newly created object doesn’t inherit from Object.prototype. So it doesn't have hasOwnProperty method.
2410-
2410+
24112411
Let's take an example to verify hasOwnProperty on an object created with create(null) function.
24122412
```javascript
24132413
const user = Object.create(null);
@@ -2505,7 +2505,7 @@ Most of these features already supported by some browsers and try out with babel
25052505

25062506
console.log(weak.get(key)); //ES2023
25072507
```
2508-
2508+
25092509
4. ### Change array by copy
25102510
Both **Array** and **TypedArray** has built-in methods such as **reverse()**, **sort()** and **splice()** to perform common actions like sorting, reverse the array elements and replacing(or removing) elements. But these methods are mutating the original array. Where as ES2023 has provided additional methods such as **toReversed()**, **toSorted()**, **toSpliced** and **with()** methods which returns new array copies instead of mutating the original array.
25112511
@@ -2707,7 +2707,7 @@ Most of these features already supported by some browsers and try out with babel
27072707
After that, the notify method awakes the waiting agent(i.e, array) that are sleeping in waiting queue and the promise is fulfilled.
27082708
27092709
Remember that, SharedArrayBuffer have been disabled on most browsers unless you specify `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers. For example,
2710-
2710+
27112711
`
27122712
Cross-Origin-Opener-Policy: same-origin
27132713
Cross-Origin-Embedder-Policy: require-corp
@@ -2718,7 +2718,7 @@ Most of these features already supported by some browsers and try out with babel
27182718
5. ### RegEx v Flag and string properties
27192719
6. ### Promise withResolvers
27202720
When you are creating a new Promise object, usually you pass `resolve` and `reject` functions to the executor of promise constructor as shown below:
2721-
2721+
27222722
```javascript
27232723
const promise = new Promise((resolve, reject) =>{
27242724
setTimeout(() => { Math.random() > 0.5 ? resolve("Success") : reject("Error")}, 1000);
@@ -2750,4 +2750,4 @@ Most of these features already supported by some browsers and try out with babel
27502750
promise.then(result => console.log(result)).catch(error => console.error(error));
27512751
```
27522752
2753-
**[⬆ Back to Top](#table-of-contents)**
2753+
**[⬆ Back to Top](#table-of-contents)**

es2015/2.arrow-functions.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
1-
//1. Single parameter and single statement
2-
var message = name => console.log("Hello, " + name + "!");
1+
// Arrow functions were introduced in ES6 (ES2015) as a new syntax for writing JavaScript functions.
2+
// They provide a more concise syntax and lexical 'this' binding.
3+
4+
// 1. Single parameter and single statement
5+
// No parentheses needed for single parameter
6+
const message = name => console.log(`Hello, ${name}!`);
37
message("Sudheer"); // Hello, Sudheer!
48

5-
//2. Multiple parameters and single statement
6-
var multiply = (x, y) => x * y;
9+
// 2. Multiple parameters and single statement
10+
// Parentheses required for multiple parameters
11+
const multiply = (x, y) => x * y;
712
console.log(multiply(2, 3)); // 6
813

9-
10-
//3. Single parameter and multiple statements
11-
var even = number => {
12-
if(number % 2) {
14+
// 3. Single parameter and multiple statements
15+
// Curly braces required for multiple statements
16+
const even = number => {
17+
if(number % 2 === 0) {
1318
console.log("Even");
1419
} else {
1520
console.log("Odd");
1621
}
17-
}
18-
even(5); // odd
22+
};
23+
even(5); // Odd
1924

20-
//4. Multiple parameters and multiple statements
21-
var divide = (x, y) => {
22-
if(y != 0) {
25+
// 4. Multiple parameters and multiple statements
26+
const divide = (x, y) => {
27+
if(y !== 0) {
2328
return x / y;
2429
}
25-
}
30+
return "Cannot divide by zero";
31+
};
2632
console.log(divide(100, 5)); // 20
33+
console.log(divide(100, 0)); // Cannot divide by zero
2734

28-
//5. No parameter and single statement
29-
var greet = () => console.log('Hello World!');
35+
// 5. No parameter and single statement
36+
// Empty parentheses required when no parameters
37+
const greet = () => console.log('Hello World!');
3038
greet(); // Hello World!
39+
40+
// 6. Lexical 'this' binding
41+
// Arrow functions don't have their own 'this' context
42+
const person = {
43+
name: 'John',
44+
regularFunction: function() {
45+
console.log(this.name); // 'John'
46+
},
47+
arrowFunction: () => {
48+
console.log(this.name); // undefined (or global 'name' if defined)
49+
}
50+
};
51+
// person.regularFunction();
52+
// person.arrowFunction();

0 commit comments

Comments
 (0)