7 JavaScript tricks learn in 5 min
7 JavaScript Tricks learn in less than 5 min

7 JavaScript Tricks learn in less than 5 min EC2

7 JavaScript Tricks learn in less than 5 min. JavaScript is one of the most popular programming languages at the moment. avaScript is the number one programming language in the world, the language of the web, of mobile hybrid apps (like PhoneGap or Appcelerator), of the server side (like NodeJS or Wakanda) and has many other implementations. You can also learn more about JavaScript and complete guide to JavaScript.

It’s important to learn the tricks and shortcut in javascript and you need to invest less than 5 min.

Create an array with unique values using the “Set” object

Imagine a array with a duplicate item and you want to show only unique items. You could try writing a map or filter to achieve this. Alternatively, ES6 introduces the Set object, which solves this problem in just 1 line of code.

const uniqueItemsarray = [...new Set([1, 2, 3, 3,])]

Create Empty Objects

Sure you can create an object that seems empty with {}, but that object still has a __proto__ and the usual hasOwnProperty and other object methods.

let dict = Object.create(null);

Merge Several Objects Together

There is always a need to merge in javascript the need to merge two or more classes, and this was my go-to approach.

const student = { 
 name: 'John Wick', 
 gender: 'Male' 
 };const school = { 
 primary: 'St Xavier School', 
 secondary: 'Paul Secondary School' 
 };const skills = { 
 programming: 'Extreme', 
 swimming: 'Average', 
 sleeping: 'Pro' 
 };const summary = {...user, ...college, ...skills};

Using the window.location object

7 JavaScript Tricks learn in less than 5 min

JavaScript can access the current URL using the window.location object. Pretty neat, but even cooler is that this object contains certain parts of the URL as well. 7 JavaScript Tricks learn in less than 5 min.

// JavaScript can access the current URL in parts. For this URL:
`https://thatsanegg.com/example/index.html?s=article`window.location.protocol == `https:`
window.location.host == `thatsanegg.com`
window.location.pathname == `/example/index.html`
window.location.search == `?s=article`

Avoid negative indexes in arrays

This is a simple trick of avoiding negative indexes

var numbersArray = [1,2,3,4,5]; 
var from = numbersArray.indexOf("foo") ;  // from is equal to -1 
numbersArray.splice(from,2);    // will return [5]

Destructuring with Aliases

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

const object = { number: 10 };
const { number } = object;// Grabbing number and renaming it as otherNumber
const { number: otherNumber } = object;console.log(otherNumber);

Pass functions, not strings, to setTimeout() and setInterval()

If you pass a string into setTimeout() or setInterval(), the string will be evaluated the same way as with eval, which is slow. Instead of using…

setInterval('doSomethingChronologically()', 1000);  
setTimeout('doSomethingAfterFiveSeconds()', 
5000);
setInterval(doSomethingPeriodically, 1000);  
setTimeout(doSomethingAfterFiveSeconds, 5000);

I hope you like this blog 7 JavaScript Tricks learn in less than 5 min. To learn more like this visit HawksCode and Easyshiksha.

Share Post

Empower Your Digital Life with HawksCode

Whether you’re looking to transform your entire IT landscape or need expertise in a specific area, HawksCode has the knowledge, experience, and resources to drive your success.

Any Questions?

Get in touch with us

Impactful Solutions

HawksCode: Your Partner in Digital Excellence