JavaScript tips

Table of Contents:

Here are few JavaScript simple tips.

In JavaScript there are true and false

These all will eval to false:


All these will be true:

123 // any number
`any string` // non-empty
[] // even empty arrays
{} // even empty objects, except null

There are no classes in JavaScript, only objects

You can use the following shorthand to create object.

var obj = new Object();

Forget about new Object()

You can forget the syntax var obj = new Object(); if you use {} like this:

var obj = {};

Similar no need to use var arr = new Array();

var arr = []; 

looks more compact.

You can use var regex = new RegExp('something'); alternative:

var regex = /something/;

How to create an anonymous function and execute it?


This single-liner first defines anonymous function in JavaScript function(){} and then executes it (). Currently this function does nothing, but you can set something inside the {}.

tags: ssh & category: javascript