8 JavaScript Tips & Tricks That No One Teaches 🚀
Tags: javascript, webdev, html, es6
Published: 2021-04-06 15:04:02

JavaScript is no doubt one of the coolest languages in the world and is gaining more and more popularity day by day. So the developer community has found some tricks and tips after using JS for quite a while now. Today I will share 8 Tips & Tricks With You!

So let's get started

## Functional Inheritance

Functional inheritance is the process of receiving features by applying an augmenting function to an object instance. The function supplies a closure scope which you can use to keep some data private. The augmenting function uses dynamic object extension to extend the object instance with new properties and methods.

They look like:
```javascript
// Base function
function Drinks(data) {
  var that = {}; // Create an empty object
  that.name = data.name; // Add it a "name" property
  return that; // Return the object
};

// Fuction which inherits from the base function
function Coffee(data) {
  // Create the Drinks object
  var that = Drinks(data);
  // Extend base object
  that.giveName = function() {
    return 'This is ' + that.name;
  };
  return that;
};

// Usage
var firstCoffee = Coffee({ name: 'Cappuccino' });
console.log(firstCoffee.giveName());
// Output: "This is Cappuccino"
```

Credits to [@loverajoel](https://twitter.com/loverajoel) for explaining this topic in depth here - [Functional Inheritance on JS Tips](https://www.jstips.co/en/javascript/what-is-a-functional-inheritance/) which I've paraphrased above

## .map() Substitute
`.map()` also has a substitute that we can use which is `.from()`:

```javascript
let dogs = [
    { name: ‘Rio’, age: 2 },
    { name: ‘Mac’, age: 3 },
    { name: ‘Bruno’, age: 5 },
    { name: ‘Jucas’, age: 10 },
    { name: ‘Furr’, age: 8 },
    { name: ‘Blu’, age: 7 },
]


let dogsNames = Array.from(dogs, ({name}) => name);
console.log(dogsNames); // returns [“Rio”, “Mac”, “Bruno”, “Jucas”, “Furr”, “Blu”]
```

## Number to string/string to number
Usually, to convert a string to a number, we use something like this:

```javascript
let num = 4
let newNum = num.toString();
```

and to convert a string to a number, we use:

```javascript
let num = "4"
let stringNumber = Number(num);
```

but what we can use to code fast is:

```javascript
let num = 15;
let numString = num + ""; // number to string
let stringNum = +s; // string to number
```

## Using length to resize and emptying an array
In javascript, we can override a built-in method called `length` and assign it a value of our choice.

Let's look at an example:
```javascript
let array_values = [1, 2, 3, 4, 5, 6, 7, 8];  
console.log(array_values.length); 
// 8  
array_values.length = 5;  
console.log(array_values.length); 
// 5  
console.log(array_values); 
// [1, 2, 3, 4, 5]
```

It can also be used in emptying an array, like this:
```javascript
let array_values = [1, 2, 3, 4, 5, 6, 7,8]; 
console.log(array_values.length); 
// 8  
array_values.length = 0;   
console.log(array_values.length); 
// 0 
console.log(array_values); 
// []
```

## Swap Values with Array Destructuring.
The __destructuring__ assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. We can also use that to swap values fast, like this:

```javascript
let a = 1, b = 2
[a, b] = [b, a]
console.log(a) // result -> 2
console.log(b) // result -> 1
```

## Remove duplicates from an Array
This trick is pretty simple. Let's say, I made an array that is containing numbers, strings, and booleans, but the values are repeating themselves more than once and I want to remove the duplicates. So what I can do is:

```javascript
const array = [1, 3, 2, 3, 2, 1, true, false, true, 'Kio', 2, 3];
const filteredArray = [...new Set(array)];
console.log(filteredArray) // [1, 3, 2, true, false, "Kio"]
```

## Short For Loop
You can write less code for a loop like this:

```javascript
const names = ["Kio", "Rio", "Mac"];

// Long Version
for (let i = 0; i < names.length; i++) {
  const name = names[i];
  console.log(name);
}

// Short Version
for (let name of names) console.log(name);
```

## Performance
In JS you can also get the time that the code was executed in like Google does:

![google example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7ed89oyhcyyjhqirvc6.png)

It looks like this:
``` javascript
const firstTime = performance.now();
something();
const secondTime = performance.now();
console.log(`The something function took ${secondTime - firstTime} milliseconds.`);
```

## ⚡️ Giveaway ⚡️
We are giving away any course you need on Udemy. Any price any course.
Steps to enter the giveaway
--> React to this post
--> Subscribe to our [Newsletter](https://worldindev.ck.page/) <-- Very important
--> Follow me on [Twitter](https://twitter.com/garvitmotwani) <-- x2 Chances of winning

The winner will be announced on May 1, Via Twitter

---

Thank you very much for reading this article.

Comment any tricks & tips you know!

**PLEASE LIKE, SHARE, AND COMMENT**

Follow me on [Dev](https://dev.to/garvitmotwani) and [Twitter](https://twitter.com/GarvitMotwani)