[Javascript] Top Javascript Tips and Tricks

Raghuraman Kesavan
Jul 27, 2017 · 2 min read

In this post, We will discuss some of the tips and tricks that every javascript developer must know.

Boolean Expression

  • null, undefined, ‘ ‘ the empty string and 0 the number are all false boolean expression.
  • But be careful, ‘0’ the string, [ ] the empty array, and {} the empty object are all true boolean expression.

so instead of using

if(x != null)

You can write the shorter format

if(x)

Similarly, if you want to check both null and empty. You can do like this:

if(x != null && x != ‘ ‘)

However, You can write short and nice

if(x)

Conditional Ternary Operator

Instead of this,

if (x) {foo();} else {bar();}

you can use the ternary operator,

return x ? foo( ) :bar( )

Short Circuit Operator

&& and ||, these operators are called as short circuit operators. Consider the following code.

function foo(x) {var win;if (x) {win = x;} else {win = window;}}

You can rewrite above code as

function foo(x) {  var win = x || window;}

&& is also used to reduce the lines of code, Please find the code below.

if (student) {  if (student.name) {    if (student.name[index]) {      foo(student.name[index]);    }  }}

You can rewrite the code as

if (student && student.name && student.name[index]) {  foo(student.name[index]);}

or, you can use a variable as,

var flag = student && student.name && student.name[index];if (flag) {  foo(student.name[index]);}

Iteration using for loop:

For loop:

var paragraphs = [1, 2, 3, 4, 5, 6];for (var i = 0; i < paragraphs.length; i++) {  var paragraph = paragraphs[i];    console.log(paragraph);}

Above code can be modified as below.

var paragraphs = [1, 2, 3, 4, 5, 6];for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {  console.log(paragraph);}

Next Article: [Angular] Quick Jump from AngularJS to Angular

If you enjoyed this article, please share with your developer friends and click the green “❤” heart below and so others can find it too. Thanks for reading.

Originally published at www.nodesimplified.com.

Bondesk.In

Share your knowledge!

Raghuraman Kesavan

Written by

Blogger@nodesimplified.com

Bondesk.In

Share your knowledge!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade