Loading

JavaScript Tips & Tricks

JavaScript Tips & Tricks

Today I am going to share you some cool tricks about javascript, let's start.

1. Number to String

How we can convert a number to string this is the easiest way to do it.

const year = 2020;
console.log(typeof year);
//Result will be "number"

//now just concat with string 
const year = 2020 + "";
console.log(typeof year);
//Result will be "string" cool!

2. Create an array with an empty string

const empty_arry = Array(2);
console.log(empty_arry);
//It will show "[ <2 empty items> ]"
const empty_array_with_blank_value = Array(2).fill("");

console.log(empty_array_with_blank_value);
//this will show the result "[ '', '' ]"

3. Remove duplicates from array in Javascript

const temp_array = [1, 2, 3, 4, 5, 4, 4, 5, 2, 1];

const unique_array = Array.from(new Set(temp_array));

console.log(unique_array);

//Result "[1,2,3,4,5]"

4. Dynamic Objects

const dynamic = 'city';
const profiles =  {
     name: "Abhay",
     email: "abhay@condinghub.net",
     [dynamic]: 'Kolkata'
 };

 console.log(profiles);

//Result will be "{ name: 'Abhay', email: 'abhay@condinghub.net', city: 'Kolkata' }"

5. Slicing Array

const numbers = [1, 2, 3, 4, 5, 6];

// if you want to slice only first 2;

numbers.length = 2;
console.log(numbers);

//Result will be [1,2]

// if you want to slice from last

console.log(numbers.slice(-2));

//This will print [5,6]

6. Array to Objects

const numbersObject = { ...numbers };
console.log(numbersObject);

//it will convert array to object & result will be "{ '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6 }"

7. Objects to Array

const profiles = {
    name: "Abhay",
    email: "abhay@condinghub.net"
};

const profilesArray = Object.values(profiles);
console.log(profilesArray);

//This will create the array and result will be  "[ 'Abhay', 'abhay@condinghub.net' ]"

8. Ternary operator

//Simple example
const salary = 1000;
salary > 500 ? console.log('Your Salary is greater than 500') : console.log("Your Salary is less than 500");
//This will print Your Salary is greater than 500

//Complex Example
const age = 11;
age > 30
    ? age > 80
        ? console.log('Your age is greater than 80')
        : console.log('Your age is between 30 to 80')
    : console.log('You are below 30');

//This will print "Your age is greater than 80"

Just change the age value and run node index.js to see the result and understand how ternary operator works.

9. Performance

 let start = performance.now();

// Your javascript code 

for (let index = 0; index < 1000; index++) {
     console.log(index);    
}

let end = performance.now();

console.log(`${end-start} took milisecond to execute` );

//copy the code & paste into browser console to see the result.

10. JavaScript Reduce Method

const fruits = [
    { name: 'apple', price: 200 },
    { name: 'mango', price: 100 },
    { name: 'banana', price: 40 }
]

const fruitPrice = fruits.reduce((a, v) => {
    let name = fruits.name;
    a[v.name] = v.price;
    return a;
}, {});

console.log(fruitPrice);

//Result will be "{ apple: 200, mango: 100, banana: 40 }"

I hope this will help you!

Related Articles