7 Killer JavaScript One-Liners that you must know

1. Generate Random String
if you will ever need a temporary unique id for something. this
one-liner will generate a random string for you

const randomString = Math.random().toString(36).slice(2);
console.log(randomString); //output- r0zf1xfqcr…


This content originally appeared on DEV Community and was authored by Kamran Ahmad

1. Generate Random String
if you will ever need a temporary unique id for something. this
one-liner will generate a random string for you

const randomString = Math.random().toString(36).slice(2);
console.log(randomString); //output- r0zf1xfqcr (the string will be random )

2. Extract Domain Name From An Email
you can use the substring() method to extract the domain name
of the email.

let email = 'xyz@gmail.com';
le getDomain = email.substring(email.indexOf('@') + 1);

console.log(getDomain); // output - gmail.com

3. Detect Dark Mode
with this one-liner, you can check if the user is using dark mode ( and then you can update some functionality according to dark mode)

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').match;

4. Check if An Element is Focused
to detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the Document object.

const elem = document.querySelector(' .text-input');

const isFocus = elem == document.activeElemnt;

/* isFocus will be true if elem will have focus, and isFocus will be false if elem will not have focus */

5. Check If An Array Is Empty
this one-liner will let you know if an array is empty or not.

let arr1 = [];
let arr2 = [2, 4, 6, 8, 10];

const arr1IsEmpty = !(Array.isArray(arr1) && arr1.length >0);
const arr2IsEmpty = !(Array.isArray(arr2) && arr2.length >0);

console.log(arr1); //output - true
console.log(arr2); // output - false

6. Redirecting User
you can redirect the user to any specific URL using JavaScript.

const redirect = url => location.href = url

/* call redirect (url) whenever you want to redirect the user to a specific url */

7. Check If A Variable Is An Array
You can check if any Variable is an Array or not using the Array.isArray() method.

let fruit = 'apple';
let fruits = ["apple", "banana", "mango", "orange", "grapes"];

const isArray = (arr) => Array.isArray(arr);

console.log(isArray.(fruit)); //output - false
console.log(isArray.(fruits)), //output- true


This content originally appeared on DEV Community and was authored by Kamran Ahmad


Print Share Comment Cite Upload Translate Updates
APA

Kamran Ahmad | Sciencx (2022-01-22T13:50:54+00:00) 7 Killer JavaScript One-Liners that you must know. Retrieved from https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/

MLA
" » 7 Killer JavaScript One-Liners that you must know." Kamran Ahmad | Sciencx - Saturday January 22, 2022, https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/
HARVARD
Kamran Ahmad | Sciencx Saturday January 22, 2022 » 7 Killer JavaScript One-Liners that you must know., viewed ,<https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/>
VANCOUVER
Kamran Ahmad | Sciencx - » 7 Killer JavaScript One-Liners that you must know. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/
CHICAGO
" » 7 Killer JavaScript One-Liners that you must know." Kamran Ahmad | Sciencx - Accessed . https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/
IEEE
" » 7 Killer JavaScript One-Liners that you must know." Kamran Ahmad | Sciencx [Online]. Available: https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/. [Accessed: ]
rf:citation
» 7 Killer JavaScript One-Liners that you must know | Kamran Ahmad | Sciencx | https://www.scien.cx/2022/01/22/7-killer-javascript-one-liners-that-you-must-know/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.