Skip to content

Commit 5d13dca

Browse files
committed
some important js build in methods 🚀
1 parent 024b9e1 commit 5d13dca

File tree

8 files changed

+68
-0
lines changed

8 files changed

+68
-0
lines changed

BuildIn-Methods/indexOf.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let str = 'Hello world, welcome to the JS Universe.';
2+
console.log(str.indexOf('welcome')); // 13
3+
console.log(str.indexOf('wall')); // -1
4+
5+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
6+
console.log(fruits.indexOf('Melon')); // 3
7+
8+
console.log(fruits.indexOf('klkljkh')); // -1

BuildIn-Methods/lastIndexOf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
2+
console.log(fruits.lastIndexOf('Melon')); // 3
3+
4+
console.log(fruits.lastIndexOf('klkljkh')); // -1

BuildIn-Methods/length.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
2+
console.log(fruits.length); // 4
3+
4+
let str = 'Hello world, welcome to the JS Universe.';
5+
console.log(str.length); // 40

BuildIn-Methods/map.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
2+
3+
const array = [1, 4, 9, 16, 15, 48];
4+
5+
// pass a function to map
6+
const map1 = array.map(ar => ar * 2);
7+
8+
console.log(map1);
9+
// expected output: Array [2, 8, 18, 32]
10+
11+
function myFunction(num) {
12+
return num * 10;
13+
}
14+
15+
let newArray = array.map(myFunction);
16+
console.log(newArray);

BuildIn-Methods/reduce.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// The reduce() method executes a reducer function (that we provide) on each element of the array, resulting in single output value.
2+
3+
const array = [1, 2, 3, 4, 5];
4+
const reducerFunc = (accumulator, currentValue) => accumulator + currentValue;
5+
6+
// 1 + 2 + 3 + 4 + 5
7+
console.log(array.reduce(reducerFunc));
8+
// expected output: 15
9+
10+
// 5 + 1 + 2 + 3 + 4 + 5
11+
console.log(array.reduce(reducerFunc, 5));
12+
// expected output: 20

BuildIn-Methods/slice.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
2+
3+
const array = [1, 2, 3, 4, 5];
4+
5+
const anotherArr = array.slice(1, 4);
6+
7+
console.log(anotherArr);
8+
9+
console.log(array.slice(2, 5));

BuildIn-Methods/toString.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let num = 10;
2+
let n = num.toString();
3+
4+
console.log(typeof num); // number
5+
6+
console.log(typeof n); // string

BuildIn-Methods/typeOf.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
console.log(typeof 44); // number
2+
3+
console.log(typeof 'something'); // string
4+
5+
console.log(typeof true); // boolean
6+
7+
let num = 12;
8+
console.log(typeof num); // number

0 commit comments

Comments
 (0)