You Think Know Javascript
You Think Know Javascript
Dev Genius
Coding, Tutorials, News, UX, UI
and muchSign
moreinrelated
to yourto
account (ok__@g__.com) for your personalized experience.
development
15 This is your last free member-only story this month. Sign up for Medium and get an extra one
Designed by www.freepik.com
1. One-liner Swapping
Every Programmer knows swapping is done with a third variable name
temp. But in JavaScript, we can swap two values in one line without any
temp variable using the destructive method.
// Swapping
let x = 900;
let y = 1000;
2. Get ByteSize
This snippet code will show you how to get the byte size of any object in
JavaScript using the built-in method.
console.log(ByteSize("Yoo")) // 3
console.log(ByteSize(23244)) // 5
3. Pro Reversing
Now you no longer need to use loops to reverse a string, This snippet will
help you to reverse any string with one line of code. Take a look at the
below code example.
// Pro Reversing
console.log(Reverse("Coding")) // gnidoC
console.log(Reverse("World")) // dlroW
4. Check AllEqual
This useful Snippet code will help you to check if the array elements are all
same or different. This comes in handy when you need to check an array of
duplicates elements.
console.log(isEqual([10,10,11,11,12,13])) // false
console.log(isEqual([10,10,10,10])) // true
5. Random Selection
Sometimes we need to select the random data from an array. Let suppose
you are making a game in JavaScript and you want to add a function that
randomly selects data from the array. Then this snippet code will be a
helpful tool for you.
// Random Selection
function Random(array){
return array[Math.floor(Math.random() * array.length )];
}
console.log(Random(array)); // Facebook
6. JSON Readability
Make your JSON data cleaner and easy to read by using the following
snippet code.
// JSON READABLE
// output
{
"name": "Haider",
"age": 22,
"class": 12
}
7. String Placeholding
You can include variables in String using this snippet code. We will do this
with the help of the ${} method. Take a look at the below code example.
// String Placeholder
// normal way
if (2 > 5){
console.log(true);
}
else{
console.log(false)
}
9. Rid of Duplicates
This snippet will help you remove the duplicates from the array. This comes
in handy when you have long elements of the array and it contains some
duplicate elements.
// Rid of Duplicates
function dup(array)
{
return [...new Set(array)];
}
// Sorting
array.sort()
console.log(capitalize("javascript")) // Javascript
console.log(capitalize("programming")) // programming
// Current Time
Final Thoughts
I hope you find this article helpful and enjoy reading it. Feel free to share
this article with your Programmer Friends. Happy JavaScript Coding!
Never stop learning! Check out my other articles, hope you find them
interesting 😃
Get the latest news and update from DevGenius publication Take a look.
15
WRITTEN BY
How to use bootstrap OnBoarding Instructions Who run the app world? Getting Started With
with Rails6 for Angular Developers React! React.js — Part 1
Chrisbradycode Jose Matos HUMAN INTERFACE Siddhant Varma in Better
TECHNOLOGIES Programming
Factories Are STILL Building an interactive Dealing with multiple Coding Serverless
Better Than Classes In Kiosk using Vue.js & Promises in JavaScript Functions in Idris
JavaScript Electron Edvinas Daugirdas in The Donald Pinckney
GreekDataGuy in JavaScript in Allen Oliver M Chun Startup
Plain English