Skip to content

Commit 51ad34d

Browse files
JS Examples
0 parents  commit 51ad34d

34 files changed

Lines changed: 19135 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*
3+
*
4+
➡️ Array#{flat,flatMap}
5+
➡️ Object.fromEntries
6+
➡️ String#{trimStart,trimEnd}
7+
➡️ Symbol#description
8+
➡️ try { } catch {} // optional binding
9+
➡️ JSON ⊂ ECMAScript
10+
➡️ well-formed JSON.stringify
11+
➡️ stable Array#sort
12+
➡️ revised Function#toString
13+
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* ECMAScript 5 Features
3+
* https://treyhunner.com/es5-talk
4+
*
5+
These were the new features released in 2009:
6+
*/
7+
8+
/**
9+
* ******************************************************
10+
* 1. Strict Mode: The "use strict" Directive
11+
* ******************************************************
12+
*/
13+
14+
15+
/**
16+
* ******************************************************
17+
* 2. String: String.prototype.trim
18+
* ******************************************************
19+
*/
20+
21+
22+
/**
23+
* ********************************
24+
* 3. Array goodies:
25+
* ********************************
26+
Array.isArray()
27+
Array.forEach()
28+
Array.map()
29+
Array.filter()
30+
Array.reduce()
31+
Array.reduceRight()
32+
Array.every()
33+
Array.some()
34+
Array.indexOf()
35+
Array.lastIndexOf()
36+
*/
37+
38+
39+
40+
/**
41+
* ********************************
42+
* 4. Native JSON support:
43+
* ********************************
44+
JSON.parse()
45+
JSON.stringify()
46+
*/
47+
48+
49+
/**
50+
* ********************************
51+
* 5. Object goodies
52+
* ********************************
53+
54+
*/
55+
56+
/**
57+
* ********************************
58+
* 6. Date goodies
59+
* ********************************
60+
JSON.parse()
61+
JSON.stringify()
62+
*/
63+
64+
/**
65+
* *********************************
66+
* 7. Accessors (getter and Setter)
67+
* *********************************
68+
69+
*/
70+
71+
/**
72+
* ******************************************************
73+
* 8. Function: Function.prototype.bind()
74+
* ******************************************************
75+
*/
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* ES6 Features:
3+
*
4+
Constants
5+
Constants
6+
Scoping
7+
Block-Scoped Variables
8+
Block-Scoped Functions
9+
Arrow Functions
10+
Expression Bodies
11+
Statement Bodies
12+
Lexical this
13+
Extended Parameter Handling
14+
Default Parameter Values
15+
Rest Parameter
16+
Spread Operator
17+
Template Literals
18+
String Interpolation
19+
Custom Interpolation
20+
Raw String Access
21+
Extended Literals
22+
Binary & Octal Literal
23+
Unicode String & RegExp Literal
24+
Enhanced Regular Expression
25+
Regular Expression Sticky Matching
26+
Enhanced Object Properties
27+
Property Shorthand
28+
Computed Property Names
29+
Method Properties
30+
Destructuring Assignment
31+
Array Matching
32+
Object Matching, Shorthand Notation
33+
Object Matching, Deep Matching
34+
Object And Array Matching, Default Values
35+
Parameter Context Matching
36+
Fail-Soft Destructuring
37+
Modules
38+
Value Export/Import
39+
Default & Wildcard
40+
Classes
41+
Class Definition
42+
Class Inheritance
43+
Class Inheritance, From Expressions
44+
Base Class Access
45+
Static Members
46+
Getter/Setter
47+
Symbol Type
48+
Symbol Type
49+
Global Symbols
50+
Iterators
51+
Iterator & For-Of Operator
52+
Generators
53+
Generator Function, Iterator Protocol
54+
Generator Function, Direct Use
55+
Generator Matching
56+
Generator Control-Flow
57+
Generator Methods
58+
Map/Set & WeakMap/WeakSet
59+
Set Data-Structure
60+
Map Data-Structure
61+
Weak-Link Data-Structures
62+
Typed Arrays
63+
Typed Arrays
64+
New Built-In Methods
65+
Object Property Assignment
66+
Array Element Finding
67+
String Repeating
68+
String Searching
69+
Number Type Checking
70+
Number Safety Checking
71+
Number Comparison
72+
Number Truncation
73+
Number Sign Determination
74+
Promises
75+
Promise Usage
76+
Promise Combination
77+
Meta-Programming
78+
Proxying
79+
Reflection
80+
Internationalization & Localization
81+
Collation
82+
Number Formatting
83+
Currency Formatting
84+
Date/Time Formatting
85+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
*ES7 Features:
3+
*
4+
Array.prototype.includes()
5+
Exponentiation operator
6+
*/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* ES8 Features:
3+
*
4+
Major new features:
5+
Async Functions (Brian Terlson)
6+
Shared memory and atomics (Lars T. Hansen)
7+
Minor new features:
8+
Object.values/Object.entries (Jordan Harband)
9+
String padding (Jordan Harband, Rick Waldron)
10+
Object.getOwnPropertyDescriptors() (Jordan Harband, Andrea Giammarchi)
11+
Trailing commas in function parameter lists and calls (Jeff Morrison)
12+
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* ES9 Features:
3+
*
4+
Major new features:
5+
Asynchronous Iteration (Domenic Denicola, Kevin Smith)
6+
Rest/Spread Properties (Sebastian Markbåge)
7+
New regular expression features:
8+
RegExp named capture groups (Gorkem Yakin, Daniel Ehrenberg)
9+
RegExp Unicode Property Escapes (Mathias Bynens)
10+
RegExp Lookbehind Assertions (Gorkem Yakin, Nozomu Katō, Daniel Ehrenberg)
11+
s (dotAll) flag for regular expressions (Mathias Bynens)
12+
Other new features:
13+
Promise.prototype.finally() (Jordan Harband)
14+
Template Literal Revision (Tim Disney)
15+
*/

js-basics/arrays.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const numbers = [1,2];
2+
3+
//**Adding new element: End */
4+
numbers.push(3,4);
5+
numbers.push(5);
6+
console.log(numbers);
7+
8+
//**Adding new element: Begining */
9+
numbers.unshift(0);
10+
numbers.unshift(0,1);
11+
console.log(numbers);
12+
13+
//**Adding new element: Middle*/
14+
numbers.splice(2,0,'a', 'b');
15+
console.log(numbers);
16+
numbers.splice(2,1,'c', 'd');
17+
console.log(numbers);
18+
/*******************************/
19+
20+
//**Finding element: Primitives */
21+
22+
console.log(numbers.indexOf('a'));
23+
console.log(numbers.indexOf(1));
24+
console.log(numbers.indexOf(1,2));
25+
console.log(numbers.indexOf('c'));
26+
27+
console.log(numbers.lastIndexOf(0));
28+
console.log(numbers.indexOf(5) !== -1);
29+
console.log(numbers.includes(1));
30+
console.log(numbers.includes('a'));
31+
32+
//**Finding element: References */
33+
34+
const courses = [{id:1, name:'a'},{id:1, name:'b'},{id:1, name:'c'}];
35+
console.log(courses.find(function(course){return course.name === 'a'}));
36+
console.log(courses.findIndex(function(course){return course.name === 'a'}));
37+
console.log(courses.find(course=>course.name === 'a'));
38+
console.log(courses.findIndex(course=>course.name === 'a'));
39+
40+
//**Removing element : End*/
41+
console.log(numbers.pop());
42+
//**Removing element: Begining */
43+
console.log(numbers.shift());
44+
//**Removing element: Middle */
45+
console.log(numbers.splice(2,1));
46+
console.log(numbers);
47+
48+
//**Empty an array*/
49+
let numbers1 = [1,2,3];
50+
let another = numbers1;
51+
let another1 = numbers1;
52+
let another2 = numbers1;
53+
numbers1 = [];
54+
console.log(numbers1);
55+
console.log(another.length=0);
56+
console.log(another);
57+
console.log(another1.splice(0, another1.length));
58+
59+
while(another2.length>0)
60+
another2.pop();
61+
console.log(another2);
62+
63+
//**Combining and slicing an arrays */
64+
65+
const first = [1,2,3];
66+
const second = [4,5,6];
67+
const third = [{id:1}];
68+
69+
const combined = first.concat(second).concat(third);
70+
console.log(combined);
71+
console.log(combined.slice(2,4));
72+
console.log(combined.slice(6,7));
73+
74+
//**Spread operator */
75+
const concatnated = [...first, ...second];
76+
console.log(concatnated);
77+
console.log(concatnated.slice());
78+
79+
//**Iterating an array */
80+
concatnated.forEach(i => console.log(i));
81+
82+
concatnated.forEach((i, index) => console.log(i, index));
83+
84+
for(let number of numbers)console.log(number);
85+
86+
//**Joining an array */
87+
console.log(concatnated.join(','));
88+
89+
//**Spliting as an array */
90+
const str = "This is a String";
91+
const splitArray = str.split(' ');
92+
console.log(splitArray);
93+
94+
console.log(splitArray.join('-'));
95+
96+
//**Sorting as an array */
97+
const numberArray = [3,1,2];
98+
numberArray.sort();
99+
console.log(numberArray);
100+
numberArray.reverse();
101+
console.log(numberArray);
102+
103+
const courses1 = [{id:1, name:'Node JS'},{id:2, name:'Java'}];
104+
console.log(courses1.sort((x, y)=>{
105+
const a = x.name.toUpperCase();
106+
const b = y.name.toUpperCase();
107+
if(a < b) return -1;
108+
if(a > b) return 1;
109+
if(a == b) return 0;
110+
}));
111+
112+
//**Testing elements of an array */
113+
const everyValues = [1,2,3,4,5];
114+
const someValues = [1,2,3,-4,5];
115+
116+
const allPositives = everyValues.every(value => value>=0);
117+
const somePositives = someValues.some(value => value>=0);
118+
119+
console.log(allPositives);
120+
console.log(somePositives);
121+
122+
console.log(someValues.every(value => value>=0));
123+
console.log(everyValues.some(value => value>=0));
124+
125+
//**Filtering an array */
126+
127+
const filteredArray = someValues.filter(value=>value>=0);
128+
console.log(filteredArray);
129+
130+
//**Mapping an array */
131+
132+
const mappedArray = filteredArray.map(value=>'<li>' + value + '</li>');
133+
const html = mappedArray.join(' ');
134+
console.log(html);
135+
136+
const mappedArray1 = someValues
137+
.filter(value=>value>=0)
138+
.map(value=>({value: value}))
139+
.filter(obj=>obj.value>1)
140+
.map(obj=>obj.value);
141+
console.log(mappedArray1);
142+
143+
//**Reducing an array */
144+
const result = someValues.reduce((accumlator, currentValue)=>accumlator+currentValue,0);
145+
console.log(result);

0 commit comments

Comments
 (0)