Another Things in Javascript that Grab My Attention

After wrote some Interesting Things in Javascript, I spent more time to learn Javascript again and I found another things that grab my attention so I write down those here. I hope it can be useful mainly for others people who are learning Javascript from other language.

Here are the things that grab my attention that I will explain in short:

  1. Strict mode
    Because of natural development of Javascript syntax and standardization, Javascript evolves from very flexible to more standardize and stricter in order to achieve more predicted and safer behaviour. Strict mode was introduced since ECMA5 and results in cleaner JavaScript, with fewer unsafe features, more warnings, also more logical behavior.We can use strict mode by adding this line at beginning of file or function:
    ‘use strict’;
    Some consequences of strict mode are:

    • We can’t use keyword this in nonmethod functions because it will return undefined. In non strict mode, this will refer to global object ( window in browser)
    • All variables must be explicitly declared in strict mode
  2. Type of null and NaN
    Interesting facts that seems weird:
    > typeof null in Javascript is object
    > typeof NaN is number
    NaN is exists to represent “not a number” but Javascript type of NaN is number? Rather weird right?
  3. Call Function with new or without
    Different return value of primitive wrapper function call with new and without new:

     Number('2')  -> will return primitive value of integer number
     new Number('2') -> will return object Number
  4. Function is object?
    I think i don’t need to explain much but show the code below that will show you function is instance of Function:

    function isThisObject(x) { return x; } console.log(isThisObject instanceof Function); //code above will show value: true
  5. Special variable to grab all parameters
    Inside function, we can use special variable ‘arguments’ with  array type to catch all arguments that pass into function
  6. Operator void in Javascript
    What is the purpose or usage of void operator?

    1. void 0 as a synonym for undefined
    2. Discard the result of expression
      Example is code to open new window below:

      javascript:void window.open("http://targeturl.com/")
    3. Prefixing an IIFE
      An IIFE must be parsed as an expression. One of several ways to ensure that is by prefixing it with void. The advantage of using prefix operators is that forgetting the terminating semicolon does not cause trouble.

      void function () { // open IIFE // inside IIFE }(); // close IIFE
  7. IIFE
    IIFE is short of Immediate Invoked Function Expression, as the name explains, it is the way to write function expression that will be immediately invoked. Mainly it is used to introduce new variable scope. The syntax of IIFE will be like this:

    (function () { // open IIFE // inside IIFE }()); // close IIFE

    Benefits and Usage of IIFE in Javascript that I excerpted from SpeakingJS book:

    • Attach private data to a function
    • Avoiding global variables; hiding variables from global scope
    • Creating fresh environments; avoiding sharing
    • Keeping global data private to all of a constructor
    • Attaching global data to a singleton object
    • Attaching global data to a method

 

 

Leave a comment

Your email address will not be published. Required fields are marked *