Skip to content

Instantly share code, notes, and snippets.

View K-Vishwak's full-sized avatar

Vishwak K-Vishwak

View GitHub Profile
const mainArr = ['apple', 'banana', 'mango', 'grapes', 'orange'];
// mainArr.slice(start, end) - start & end are indexes. JS index starts with 0.
const shallowArr = mainArr.slice(1, 4);
// includes 1 & excludes 4.
shallowArr
> ['banana', 'mango', 'grapes']
const onlyStart = mainArr.slice(3);
const mainArr = ['apple', 'banana', 'mango', 'grapes', 'orange'];
const negativeIndex = mainArr.slice(-4);
// 1:
negativeIndex;
> ...........
const ObjectsArray = [
{id: '1', name:'vk'},
{id: '2', name:'js'}
];
const sliced = ObjectsArray.slice(1);
sliced
> 0: {id: '2', name: 'js'}
const str = 'Welcome to vkglobal just code';
// copies entire string.
str.slice();
> 'Welcome to vkglobal just code'
// between indexes. Count spaces too.
str.slice(11,19);
> 'vkglobal'
const str = 'Welcome to vkglobal just code';
const negativeToPositiveRange = str.slice(-11, 10);
// 1:
negativeToPositiveRange;
> ......................
const num = 10.5;
console.log(Math.round(num)); // 11
console.log(Math.ceil(num)); // 11
console.log(Math.floor(num)); // 10
const num1 = 10.4;
console.log(Math.round(num1)); // 10
console.log(Math.ceil(num1)); // 11
const num = 10.547;
console.log(Math.trunc(num));
> 10
const num = 10.547;
const num1 = -10.547;
console.log(Math.abs(num)); // 10.547
console.log(Math.abs(num1)); // 10.547
const num = 9;
console.log(Math.pow(num, 2)); // 81
console.log(Math.sqrt(num)); // 3
console.log(Math.max(3,4,5,1,2)); // 5
console.log(Math.min(3,4,5,1,2)); // 1