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.”

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.

// 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:

parseInt("hello", 10); // NaN

// Test for NaN
isNaN(NaN); // true

JavaScript also has the special values Infinity and -Infinity:

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:

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

// + operator
"3" + 4 + 5; // "345"
3 + 4 + "5"; // "75"

// == operator
123 == "123"; // true
1 == true; // true

🐮 Functions

function add(x, y) {
var total = x + y;
return total;
}

add(); // NaN
add(2, 3, 4); // 5
function add() {
var sum = 0;
for(var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}

add(2, 3, 4, 5); // 14
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

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

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!