JavaScript Learning Notes

1. JavaScript Objects

  1. Objects are variables too, but objects can contain many values.

    1
    var car = {type:"flat", model:"500",color:"white"}

    1.1 The value are written as name:value pairs
    1.2 JavaScript objects are container for named value.

  2. Two ways to access objects:

    2.1.

    1
    objectName.propertyName

    2.2.

    1
    objectName["propertyName"]

2. JavaScript Scope

  1. In JavaScript, scope is the set of variables,objects,and functions you have access to.

  2. Local JavaScript Variables

    • Variables declared within a JavaScript function, become LOCAL to the function.
    • Local variable have local scope: They can be accessed within the function.
    • Local variables are created when a function starts, and deleted when the function is completed.
  3. Global JavaScript Variables

    • Variables declared outside a function, becomes Global.
    • A global variable has global scope: All scripts and functions on a web page can access it.
  4. Automatically global

    • If you assign a value to a variable that has not been declared, it will automatically become a GOLBAL variable.

3. JavaScript String methods

  1. Finding a String in a String

    • The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a String:

      1
      2
      var str = "Please locate where 'locate' occurs!";
      var pos = str.indexOf("locate");
    • The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.

  2. Searching for a String in a String

    • The search() method searches a string for a specifed value and returns the position of the match.
  3. Extracting String parts

    • slice(start,end)
    • substring(start,end) can’t accept negative indexes
    • substr(start,length)
  4. Replacing String Content

    • replace() method replaces a specified value with another value in a string.
    • By default, the replace()function replaces only the first match. To replace all matches, use a regular expression with a g flag(for global match).

      1
      2
      str = "Please visit Apple!";
      var n = str.replace(/Apple/g,"Microsoft!");
    • The replace() method does not change the string it is called on, it returns a new string.

  5. Converting to Upper and Lower Case

    • toUpperCase()
    • toLowerCase()
  6. concat() method

    • concat() joins two or more strings:
      1
      2
      3
      var text1 = "Hello!";
      var text2 = "World!";
      text3 = text1.concat("",text2);
  7. Extracting String Characters

    • There are 2 safe methods for extracting string characters:
      • charAt(position) returns the character at a specified index in a string
      • charCodeAt(position) returns the unicode of the character at a specified index in a string.
  8. Converting a String to a Array

    • A string can be converted to an array with the split() method:
      1
      2
      3
      4
      var text = "a,b,c,d,e";
      text.split(","); // Split on commas
      text.split(" "); // Split on space
      text.split("|"); // Split on pipe

4. JavaScript Array Methods

  1. Converting Arrays to Strings
    • toString() converts an array to a string of(comma separated) array values.
      1
      2
      var fruits = ["banana", "orange", "Apple", "Mango"];
      fruits.toString();
  • join() method also joins all array elements into a string.
    1
    2
    var fruits = ["banana", "orange", "apple", "Mango"];
    fruits.join("*");
  1. Popping and pushing

    • pop() method removes the last element from an array
    • push() method adds a new element to an array(at the end)
  2. Shifting Elements

    • shift() is equivalent to popping, working on the first element instead of the last.
    • unshift() methods adds a new element to an arry(at the beginning), and “unshifts” older elements.
  3. Changing Elements

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
       var fruits = ["banana", "orange", "apple", "Mango"];
    fruits[fruits.length] = "kiwi";
    ```

    5. Deleting Elements

    * Using Delete may leave undefine holes in the array. Use pop() or shift() instead.

    ``` JavaScript

    var fruits = ["banana", "orange", "apple", "Mango"];
    delete fruits[0]; // Changes the first element in the fruites to undefined.
  1. Splicing an Array

    • The splice() method can be used to add new items to an array:
    1
    2
    var fruits = ["banana", "orange", "apple", "Mango"];
    fruits.splice(2,0,"Lemon","Kiwi");
    • The first parameter (2) defines the position where new elements should be added(spliced in).
    • The second parameter (0) defines how many elements should be removed.
    • The rest of the parameter(“Lemon”,”Kiwi”) define the new elements to be added.
  2. Joining Arrays

    • concat() method creates a new array by concatenating two arrays.
  1. Slicing an Array
    • slice() method slices out a piece of an array into a new array.
    • The slice() method creates a new array. It does not remove any elements from the source array.

5. JavaScript Sorting Arrays

  1. Sorting an Array

    • sort() method sorts an array alphabetically:
      1
      2
      var fruits = ["Banana","Apple","Orange","Mango"];
      fruits.sort();
  1. Reversing an Array

    • reverse method reverses the elements in an array.
  2. Numeric Sort

    • The compare function
      • The purpose of the compare function is to define an alternative sort order.
      • The compare function should return a negative, zero, or positive value, depending on the arguments:
        1
        function(a,b) {return a-b}
Share Comments