Useful JavaScript One-liners

JavaScript syntax and built-in methods allow us to cut down a lot of unnecessary lines in our code and write short, easily readable code.Although java script is more famous for its dynamic nature, it also has many other great features.

Hide an element

let hide = (element) => element . style.display =”none”;

Remove duplicates from array

let removeDuplicate = (arr) => [.. .new Set(arr)];

console. log( removeDuplicate( [3,5,44,9,44, 78,44]));

/ /[3,5, 44, 9, 78]

Ternary Operator

let age = 17;

let checkAge = (age >= 18) ? “You are eligible for vote” “You are not eligible for vote”;

console. log( checkAge)

/ / You are not eligible for vote

Check if a string is Uppercase

let uppercase = (str) => str == str. toUpperCase( );

console. log( uppercase ( “HTML”) )

//True

Reverse a string

let reverseStr = str => str.split(”).reverse().join(” “);

console. log( reverseStr( “html”));

//html

Leave a Reply

Your email address will not be published.