DEV Community

Mark Vassilevskiy
Mark Vassilevskiy

Posted on

6 Tricks in JavaScript That Will Make You Feel Like a Pro

JavaScript is the most popular programming language in the world and its popularity of it only increases over time. It is usually used in Fron-end, Back-end development, however, there're many libraries, frameworks, and virtual environments which give you the ability to build everything that you want. 
For example, with React Native you're able to create a mobile app for iOS and Android at the same time, and as you can understand it increases your efficiency a lot. So yeah, JavaScript is kind of in demand right now, and knowing the tricks that I will show you in this article will be really useful, especially if you want to impress someone. Keep reading and know a bunch of new stuff!


1. Arrow Functions

Of course, the first trick on the list is Arrow Function and if you don't know about it, then you surely need to. With this, you'll write your functions in a more productive and easier way so your code will decrease in its size in half. Also, it will be easier to read and understand your code. See the example:

//Usual Function
hello = function(val) {
  return "Hello World!" + val;
}
//Arrow Function
hello = (val) => "Hello " + val;
Enter fullscreen mode Exit fullscreen mode

2. Convert to String, Number, Boolean

Converting different types of data into another one might be very useful in many specific situations or when you just need to convert it so you can concatenate it later with other types of data. Here you can see examples of them:

//Converting to a string
let x = 1 + "";
console.log(x); // Result: "1"
//Converting to a number
let y = "25";
y = +y;
console.log(y); // Result: 15
//Converting to a boolean
const z = !0;
console.log(typeof z); // Result: "boolean"
Enter fullscreen mode Exit fullscreen mode

3. Math() operators' replacement

Math() operators are really useful and with this, you can write really hard mathematical algorithms and use the full power of Math. However, if you want to do some easy tasks such as Powering, and Rounding it will be much better and more efficient to use vanilla JS, without any libraries. Look at this example:

//Powering::Before
console.log(Math.pow(2,3)) //Result: 8
//Powering::after
console.log(2 ** 3) //Result: 8
And also, if you want to round your number there's a quick solution for it too. You don't need to use Math.floor() , Math.ceil() or Math.round() to round anymore, here it is:
//Rounding::Before
console.log(Math.floor(47.6)) //Result: 47
//Rounding::after
console.log(47.6 | 0) //Result: 47
Enter fullscreen mode Exit fullscreen mode

4. Quick Console.log

If you've been always writing console.log() in full length, then trust me, I'll save you lots of time with this really simple trick:

let c = console.log.blind(document);
c("Hello World"); //Result: "Hello World"
c(123); //Result: 123
c(True); //Result: True
Enter fullscreen mode Exit fullscreen mode

5. Remove Final Digits

You can also use the 'or' operator to remove any number of digits from the end of the integer. This means you don't have to write a long line of code just to remove a single digit from your integer. See the example:

//Before
let str = "2022"; 
Number(str.substring(0, str.length - 1)); //Result: 202
//After
console.log(2022 / 10   | 0)  // Result: 202
console.log(2022 / 100  | 0)  // Result: 20
console.log(2022 / 1000 | 0)  // Result: 2
Enter fullscreen mode Exit fullscreen mode

6. Numerical Separator

If you're working with long numbers and always trying to understand whether it's a 1,000,000 or 10,000,000 and there's always a problem because no one knows about this method to make your numbers much more readable and good-looking. We'll use "_" as a numerical separator in this example:

//Before
let x = 1000000
let y = 10000000
//After
let x = 1_000_000
let y = 10_000_000
//The output will be the same for the both example
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it, now you know 6 crucial tricks and tips in JavaScript that will not only increase your productivity and also will be helpful in lots of situations and will make your code easier to read. I tried to find the greatest ones and tell you about them, if you liked this article then don't forget to follow me on Twitter and leave an appreciation!

Top comments (0)