Re-Start to Learn JavaScript by MDN tutorial



I read the following tutorial for re-introduction to JavaScript:

A re-introduction to JavaScript (JS tutorial) - JavaScript


Introduction

JavaScript was created in 1995 by Brendan Erich, who worked at Netscape.
It was originally called LiveScript at first, but it was renamed to JavaScript after an ill-fated marketing decision.

Overview

JavaScript’s diagram looks like this:

  • Number
  • String
  • Boolean
  • Symbol (new in Edition 6)
  • Object
    • Function
    • Array
    • Date
    • RegExp
  • Null
  • Undefined
  • Error (Built-in)

Number

Numbers in JavaScript are “double-precision 64-bit format IEEE 754 values.”

1
0.1 + 0.2 // 0.30000000000000004

You can convert a string to an integer using the built-in parseInt() function.
In old browsers strings beginning with a “0” are assumed to be octal (radix 8), but this hasn’t been the case since 2013.

1
2
3
4
5
6
7
8
9
10
// Modern browsers:
parseInt("123", 10); // 123
parseInt("010", 10); // 10
// Old browsers:
parseInt("010"); // 8
// Interesting examples:
parseInt("0x10"); // 16
parseInt("11", 2); // 3

A special value called NaN (short for “Not Number”) is returned:

1
2
3
4
parseInt("hello", 10); // NaN
// Test for NaN
isNaN(NaN); // true

JavaScript also has the special values Infinity and -Infinity:

1
2
3
4
5
6
7
1 / 0; // Infinity
-1 / 0; // -Infinity
// Test for Infinity, -Infinity, NaN
isFinite(1/0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

Boolean

Any value can be converted to a boolean according to the following rules:

  1. false, 0, empty strings (“”), NaN, null, and undefined all become false.
  2. All other values become true.

Examples of Boolean() function as follows:

1
2
3
4
5
6
Boolean(""); // false
Boolean(NaN); // false
Boolean(undefined); // false
Boolean(null); // false
Boolean(234); // true
Boolean("234"); // true

Boolean operations such as && (logical and), || (logical or), and ! (logical not) are supported.
So, we don’t frequently need to use it.

Operator

1
2
3
4
5
6
7
// + operator
"3" + 4 + 5; // "345"
3 + 4 + "5"; // "75"
// == operator
123 == "123"; // true
1 == true; // true

Functions

1
2
3
4
5
6
7
function add(x, y) {
var total = x + y;
return total;
}
add(); // NaN
add(2, 3, 4); // 5
1
2
3
4
5
6
7
8
9
function add() {
var sum = 0;
for(var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
add(2, 3, 4, 5); // 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = function() {
return this.first + " " + this.last;
};
this.fullNameReversed = function() {
return this.last + ", " + this.first;
};
return this;
}
var p = new Person("Simon", "Willison")
p.fullName(); // "Simon Willison"
p.fullNameReversed(); // "Willison, Simon"

Add a method to String by the prototype of built-in JavaScript

1
2
3
4
5
6
7
8
9
10
11
var s = "Simon";
String.prototype.reversed = function reversed() {
var r = '';
for(var i = this.length - 1; i >= 0; i --) {
r += this[i];
}
return r;
}
s.reversed(); // nomiS