0% found this document useful (0 votes)
4 views20 pages

Javascript For Pros

Uploaded by

katarichallenge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Javascript For Pros

Uploaded by

katarichallenge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

18 JavaScript One-Liners

That'll Make You Look Like a


PRO

@DimpleKumari
Forming a network of fantastic coders.
1. Generate a random string
We can use Math.random to generate a random string, it's very
convenient when we need a unique ID.

@DimpleKumari
Forming a network of fantastic coders.
2. Escape HTML special characters
If you know about XSS, one of the solutions is to escape HTML
strings.

@DimpleKumari
Forming a network of fantastic coders.
3. Uppercase the first character of
each word in a string
This method is used to uppercase the first character of each
word in a string.

@DimpleKumari
Forming a network of fantastic coders.
4. Convert a string to camelCase

@DimpleKumari
Forming a network of fantastic coders.
5. Remove duplicate values in an array
It is very necessary to remove the duplicates of the array,
using "Set" will become very simple.

@DimpleKumari
Forming a network of fantastic coders.
6. Flatten an array
We are often tested in interviews, which can be achieved in two
ways.

@DimpleKumari
Forming a network of fantastic coders.
7. Remove falsy values from an array
Using this method, you will be able to filter out all the falsy
values from the array.

@DimpleKumari
Forming a network of fantastic coders.
8. Check if a number is even or odd
Super simple task that can be solved by using the modulo
operator (%).

@DimpleKumari
Forming a network of fantastic coders.
9. Get a random integer between two
numbers
This method is used to get a random integer between two
numbers.

@DimpleKumari
Forming a network of fantastic coders.
10. Get average value of arguments
We can use the reduce method to get the average value of the
arguments that we provide in this function.

@DimpleKumari
Forming a network of fantastic coders.
11. Truncate a number to a fixed decimal
point
Using the Math.round() method, we can truncate a number to a
certain decimal point that we provide in the function.

@DimpleKumari
Forming a network of fantastic coders.
12. Calculate the number of difference
days between two dates
Sometimes we need to calculate the number of days between two
dates, a line of code can be done.

@DimpleKumari
Forming a network of fantastic coders.
13. Get the day of the year from a date
Do you want to know the day of the year a certain date is?

@DimpleKumari
Forming a network of fantastic coders.
14. Generate a random hex color
If you need a random color value, this function will do.

@DimpleKumari
Forming a network of fantastic coders.
15. Convert RGB color to hex

@DimpleKumari
Forming a network of fantastic coders.
16. Clear all cookies

@DimpleKumari
Forming a network of fantastic coders.
17. Detect dark mode

@DimpleKumari
Forming a network of fantastic coders.
18. Swap two variables

@DimpleKumari
Forming a network of fantastic coders.
Dimple Kumari
Forming a network of fantastic coders.

You might also like