Monday 2 January 2017

My notes on JavaScript

I started study JavaScript about one years ago, I studied the basic rule and then, due to my work, I moved quickly to use some framework (Ionic and Angular) so I omitted to study it in depth and also  the best practise... Because of this bad start, this December, I re-started to study and I'm writing some notes (mainly on different things than other languages).

Variables

  • keyword const, to declare a constant.
  • Number:
    • Number.NEGATIVE_INFINITY<Number.MIN_VALUE<Number.MAX_VALUE<Number.POSITIVE_INFINITY.
    • Math is an "object" with the most common mathematic functions.
    • Some useful method of Number: parseInt(), parseFloat(), isNaN(), isInfinite().
  • Multiline string, useful to include variable values into the string:
        var a=2;
        var b=2;
        var myString=`this is
                      my value ${a+b} `;

The if statement

  • To test if an array is empty, simply use if(myArray.length) not if(myArray.length>0).
  • To test if a string is void use if(myString).
  • Test only if an object is null if(myObj===null) not if it's null and undefined.

The scope

The scope is a "nested chain": the variable definition is searched in the nested scope, if there isn't then it's search in the upper scope.

Functions

The functions parameters are stored in an object (arguments), similar to an array, so it's possible to retrive parameters with: arguments[i].

Collections

  • Array
    • it can stores several type.
    • it's a map, the key is the index. The length() method return the last index plus 1, so 
          var a=2;
          var myArray=[];
          myArray[25]="myLastObject";
          console.log(myArray.length()); //print 26
      
    • Usefull methods: foreach, concat, join, pop, push, shift, unshift, reverse, sort.
  • Maps, only in ES6.
  • Sets, only in ES6.

Tools

  • bower, package manager.
  • npm, pakage manager.
  • javascriptlint
  • gulp, builder, it's possible run several task (lint, minify resources..).

No comments:

Post a Comment