10 JavaScript Hidden Hacks You Probably Never Heard
Introduction
JavaScript is a versatile and dynamic language that has allowed developers to create some of the most innovative and complex applications on the web.
As the language evolved, new features and techniques were introduced to help developers write more efficient and readable code.
Although some of them are well known and widely used, there are many hidden features in the language that can have a significant impact on your programming skills.
From advanced features from manipulating strings to manipulating arrays, and from working with numbers and floats to handling promises, we cover a variety of topics that developers often overlook. You can use these features to streamline your code and streamline your development workflow.
Whether you’re an experienced developer or new to JavaScript, this article blog will help you help you provide valuable information to improve your programming skills. So let’s dive in and explore these JavaScript functions!
Read More : How Do I Remove A Property From A JavaScript Object?
Arrays
1. Array.flat()
As the name suggests, this method is used to collapse an array of nested arrays. For example:
const arr = [[1, 2], [3, 4], [5, 6]];
console.log(arr.flat()); // [1, 2, 3, 4, 5, 6]
2. Array.flatMap()
In Suppose you want in the example above, multiply all elements by any number, then reduce the whole array, this method will be useful for you.
This method is a combination of Array.map()
and Array.flat( )
Methods. It is used to assign a new value to each element of an array and then shrink the resulting array. For example:
const arr = [[1, 2], [3, 4], [5, 6]];
const result = arr.flatMap(item => item.map(value => value * 5));
console.log(result); // [5, 10, 15, 20, 25, 30]
3. Array.reduceRight()
This method is used to shrink a table from right to left. It can be used to simplify code when working with arrays that need to be processed from right to left. For example:
const arr = [1, 2, 3, 4, 5];
const result = arr.reduceRight((accumulator, currentValue) => {
return accumulator - currentValue
}, 0);
console.log(result);
Read Also : How JavaScript Works: An Overview Of The Engine, The Runtime, And The Call Stack?
4. TimeStamp in JavaScript
Did you know that in JavaScript we have many ways get date using Date method? See sample code below.
// original method
var date = new Date()
timestamp = date.getDate()
console.log(timestamp)
// shorter method
timestamp = new Date().getDate()
console.log(timestamp)
// shortest method
timestamp += new Date();
console.log(timestamp)
5.Delete a value in the matrix
Usually we use the Remove method to remove an element from an array. But it creates a hole in the matrix. Undefined is inserted into the element’s distance index.
We can use the splice method to do some work, but it completely removes the index from the array without leaving any spaces.
// synatax : splice(array index, number of value to delete )
var array = [1, 2, 3, 4, 5, 6]
//delete method
delete array[4]
//splice method
array.splice(4,1)
console.log(array) // [1, 2, 3, 4, 6]
6. IN Operator in JavaScript
Using In the
operator, you can check whether or not a key exists in the object. This is useful when you want to check whether a specific key exists or not in an object.
var a = 4
var b = 5
var list = {1:7, 3:9, 4:0, 2:9}
console.log(a in list) //true
console.log(b in list) // false
7. JavaScript String Padding
JavaScript Padding is used to add padding in the text of the string. We can add padding at the beginning or at the end of the string. This is the syntax for padStart
and padEnd.
padStart(targetLength, padString(optional))
padEnd(targetLength, padString(optional))
`PadString` is an optional argument for both padding methods. Below is sample code to understand how it works.
console.log("123".padStart(5)) // 123
console.log("123".padStart(5, "0")) // 00123
console.log("123".padEnd(5, "0")) // 12300
console.log("123".padEnd(10, "0")) // 1230000000
8. includes()
I bet most of you are using indexOf
to find an element in a painting. Do not use this method. We have a better way to do the same job. Use include
method instead of indexOf
, which returns the boolean result.
var array = ["Python", "JavaScript", "C++", "Dart", "JAVA"]
console.log(array.includes("JavaScript")) //True
console.log(array.includes("C#")) // false
9. Redirect to URL
JavaScript had a method that can redirect you to a website in the browser as soon as you run the code. This is useful when a user performs an action on websites and JavaScript directs the user to different url redirects.
const Redirect = (url, asLink = true) => asLink ? (window.location.href = url) : window.location.replace(url);
redirect('https://yourquorum.com')
10. Convert float to integer (quick way)
To convert float to integer you had to use `Math.floor()`, `Math.round()` and `Math.ceil()` methods, but you can to use Mit | you can use a bitwise OR operator to make the conversion faster.
// old way
console.log(Math.floor(23.56))
// Quick way
console.log(23.56 | 0)
Conclusion
I hope you found these JavaScript functions and tricks useful and interesting, and that you find some new tricks. I would love to read it in the comments section.
If you found this article useful, please click the ❤️ button below and share the article on one of your social media blogs. So that your friends can also enjoy it.
More information
If you liked this article, you will also find my other articles interesting. Check out my saas Product.