Advanced JavaScript Notation


The following javascript examples and notation demonstrate a few alternatives to well known methods for common tasks.

Advanced Notation

Tilde Operator

The tilde operator “~” literally equates to “-(n+1)”.  For example:

var a = ~1; //returns -2
var myString = "hello world";
~myString.indexOf("hello"); //returns true

Large Denary Numbers

Large denary numbers can be represented in short hand notation using the “e” operator.  For example:

1e6; //returns 1000000.

Floor Checking

Math.floor is the traditional way to check for floor number values.  There are two other short hand methods for performing the same operation, “0|” and “~~”. For example:

var n = 1.23;
Math.floor(n); //returns 1
~~n; //returns 1
0|n; //returns 1


When checking for infinity you can use “Infinity” or you can use “1/0”. For example:

Infinity == 1/0; //returns true;

Comma Chaining

The “,” can be used to chain statements together.  For example:



Another way to round numbers up is to use “n+.5|0” which is the equivalent to Math.round.  However, this shortcut only works for positive numbers.  For example:

3.2+0.5|0; //returns 3
3.5+0.5|0; //returns 4

String Linking

Strings have a built in method that will trans form them into a link and return the HTML.  For example:

"godlikemouse".link(""); //returns <a href="">godlikemouse</a>

Bit Shifting

A quick way to divide by 2, or to raise something to the power of two is to shift the value. For example

50>>1; //returns 25
20>>1; //returns 10
2<<1; //returns 4
8<<1; //returns 16

Leave a Reply
