JavaScript Tips - 1

1 min read Original article ↗

Few JavaScript tips which are handy

1. Filter only strings from an array

let arr = [‘one’, ‘two’, {a: 10}];

arr.filter(i => typeof i === ‘string‘) // [‘one‘, ‘two‘]

2. Optional chaining

Easier way to remember optional chaining is that the ‘?’ works on the prefix

obj?.func?.() // checks/evaluates function ‘func‘ and object ‘obj‘ exists

3. Null coalescing operator

Checks the left hand value and if its null or undefined, returns the right hand value

const a = null ?? ‘default value‘;

console.log(a); // ‘default value‘

That is it for part 1. Stay tuned.

Discussion about this post

Ready for more?