Forword

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:

1
2
3
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:

1
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:

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

Infinity

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

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

Comma Chaining

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

1
with(document.body)style.backgroundColor="#fff",style.color="#000"

Rounding

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:

1
2
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:

1
2
"godlikemouse".link("https://www.godlikemouse.com");
//returns <a href="https://www.godlikemouse.com">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:

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