Wednesday, July 11, 2018

Ecma script summary -GoodOne !

Following objects have been introduced in javascript:

  1. Date
  2. Arrays
  3. Map
  4. Set
  5. Strings


Strings
  • There are 3 types of quotes. Backticks allow a string to span multiple lines and embed expressions.
  • Strings in JavaScript are encoded using UTF-16.
  • We can use special characters like \n and insert letters by their unicode using \u....
  • To get a character, use: [].
  • To get a substring, use: slice or substring.
  • To lowercase/uppercase a string, use: toLowerCase/toUpperCase.
  • To look for a substring, useindexOf, or includes/startsWith/endsWith for simple checks.
  • To compare strings according to the language, use: localeCompare, otherwise they are compared by character codes.
There are several other helpful methods in strings:
  • str.trim() – removes (“trims”) spaces from the beginning and end of the string.
  • str.repeat(n) – repeats the string n times.
  • …and more. See the manual for details.
Strings also have methods for doing search/replace with regular expressions. But that topic deserves a separate chapter, so we’ll return to that later.



Java Script Arrays

Things to Remember;
  • If we shorten length manually, the array is truncated.
We can use an array as a deque with the following operations:
  • push(...items) adds items to the end.
  • pop() removes the element from the end and returns it.
  • shift() removes the element from the beginning and returns it.
  • unshift(...items) adds items to the beginning.
To loop over the elements of the array:
  • for (let i=0; i – works fastest, old-browser-compatible.
  • for (let item of arr) – the modern syntax for items only,
  • for (let i in arr) – never use.

  • To add/remove elements:
    • push(...items) – adds items to the end,
    • pop() – extracts an item from the end,
    • shift() – extracts an item from the beginning,
    • unshift(...items) – adds items to the beginning.
    • splice(pos, deleteCount, ...items) – at index pos delete deleteCount elements and insert items.
    • slice(start, end) – creates a new array, copies elements from position start till end (not inclusive) into it.
    • concat(...items) – returns a new array: copies all members of the current one and adds items to it. If any of items is an array, then its elements are taken.
  • To search among elements:
    • indexOf/lastIndexOf(item, pos) – look for item starting from position pos, return the index or -1if not found.
    • includes(value) – returns true if the array has value, otherwise false.
    • find/filter(func) – filter elements through the function, return first/all values that make it return true.
    • findIndex is like find, but returns the index instead of a value.
  • To transform the array:
    • map(func) – creates a new array from results of calling func for every element.
    • sort(func) – sorts the array in-place, then returns it.
    • reverse() – reverses the array in-place, then returns it.
    • split/join – convert a string to array and back.
    • reduce(func, initial) – calculate a single value over the array by calling func for each element and passing an intermediate result between the calls.
  • To iterate over elements:
    • forEach(func) – calls func for every element, does not return anything.
  • Additionally:
    • Array.isArray(arr) checks arr for being an array.
Please note that methods sortreverse and splice modify the array itself.
These methods are the most used ones, they cover 99% of use cases. But there are few others:
  • arr.some(fn)/arr.every(fn) checks the array.
    The function fn is called on each element of the array similar to map. If any/all results are true, returns true, otherwise false.
  • arr.fill(value, start, end) – fills the array with repeating value from index start to end.
  • arr.copyWithin(target, start, end) – copies its elements from position start till position end into itself, at position target (overwrites existing).
For the full list, see the manual.
From the first sight it may seem that there are so many methods, quite difficult to remember. But actually that’s much easier than it seems.
Look through the cheatsheet just to be aware of them. Then solve the tasks of this chapter to practice, so that you have experience with array methods.
Afterwards whenever you need to do something with an array, and you don’t know how – come here, look at the cheatsheet and find the right method. Examples will help you to write it correctly. Soon you’ll automatically remember the methods, without specific efforts from your side.

________________________________________________________________________________


String methods

String length



alert( `My\n`.length ); // 3
  • Accessing characters


  • let str = `Hello`;
    
    // the first character
    alert( str[0] ); // H
    alert( str.charAt(0) ); // H
    
    // the last character
    alert( str[str.length - 1] ); // o


  • for (let char of "Hello") {
      alert(char); // H,e,l,l,o (char becomes "H", then "e", then "l" etc)
    }

  • Strings are immutable

let str = 'Hi';

str[0] = 'h'; // error
alert( str[0] ); // doesn't work
alert( 'Interface'.toUpperCase() ); // INTERFACE
alert( 'Interface'.toLowerCase() ); // interface
Or, if we want a single character lowercased:
alert( 'Interface'[0].toLowerCase() ); // 'i'
  • There are 3 types of quotes. Backticks allow a string to span multiple lines and embed expressions.
  • Strings in JavaScript are encoded using UTF-16.
  • We can use special characters like \n and insert letters by their unicode using \u....
  • To get a character, use: [].
  • To get a substring, use: slice or substring.
  • To lowercase/uppercase a string, use: toLowerCase/toUpperCase.
  • To look for a substring, use: indexOf, or includes/startsWith/endsWith for simple checks.
  • To compare strings according to the language, use: localeCompare, otherwise they are compared by character codes.
There are several other helpful methods in strings:
  • str.trim() – removes (“trims”) spaces from the beginning and end of the string.
  • str.repeat(n) – repeats the string n times.
  • …and more. See the manual for details.
Strings also have methods for doing search/replace with regular expressions. But that topic deserves a separate chapter, so we’ll return to that later.

_________________________________________________________________________________

Error Handling in JavaScript

try {
  lalala; // error, variable is not defined!
} catch(err) {
  alert(err.name); // ReferenceError
  alert(err.message); // lalala is not defined
  alert(err.stack); // ReferenceError: lalala is not defined at ...

  // Can also show an error as a whole
  // The error is converted to string as "name: message"
  alert(err); // ReferenceError: lalala is not defined
}

____________________________________________________________________________


Custom Error

https://javascript.info/custom-errors


____________________________________________________________________________























No comments:

Post a Comment