Awesome tricks every Javascript coder should know

Manpreet Singh
2 min readSep 29, 2021

Welcome back! Javascript is an awesome programming language with a ton of capability, so let’s talk about some awesome tricks every Javascript developer should know. Now, if you’re a beginner to Javascript, check out their website below to learn more:

Let’s take a look at a few of these tricks!

Trimming Strings

An awesome trick with Javascript is trimming whitespaces from strings, to do this we would essentially use the trim function:

var strings = '    test    '
strings.trim();
The output would be just 'test'

Getting Random Items From Array

Next up, one of my favorite tricks with Javascript is getting a random item from an array, we can do this by using the Math.random() function and the .length function:

//let's say we had the following array
var array1 = [1,2,'one',2,3,'two'];//we can now use the above functions to pull a random item
var item = array1[Math.floor(Math.Random() * array.length)];

--

--