INTRODUCTION

What is JavaScript?

JavaScript is the programming language of the Web

STATEMENTS

All JavaScript variables must be identified with unique names
These unique names are called identifiers
Identifiers can be short names (x, y, z) or more descriptive names (age, sum, totalVolume)
Names can contain letters, digits, underscores, and dollar signs ($my_Identifier1)
Names must begin with a letter, begin with numbers are invalid (myIdentifier1)
Names can also begin with $ and _ ($myIdentifier, _myIdentifier)
Names are case sensitive (myIdentifier ≠ myidentifier)
Reserved words cannot be used as names (var, let, const, new are invalid identifiers)

Automatically

Any undeclared identifiers, they are automatically declared when first used
x = 5; // x stores value 5
y = 10; // y stores value 10
z = x + y; // z performs addition of 5 and 10, stores value 15
Best practice to always declare identifiers before use

var myIdentifier

// Hoisted Identifiers
x = 1; // Identifier x stores value 1
var x = 2; // Identifier x is initialized with var keyword, stores new value 2

// Re-initialized Identifiers
var x = 1; // Identifier x is initialized with var keyword, stores value 1
var x = 2; // Identifier x is re-initialized with var keyword, stores new value 2

// Arithmetic Operations
var x = 1; // Identifier x is initialized with var keyword, stores value 1
x += 2; // Identifier x performs addition of 1 and 2, stores new value 3

// Global Scope
var x = 1; // Identifier x is initialized with var keyword outside the block scope, stores value 1
{ var x = 2; } // Identifier x is initialized with var keyword within the block scope, stores value 2
console.log(x); // Identifier x is 2, since the var keyword is a global scope

let myIdentifier

// Hoisted Identifiers
x = 1; // Identifier x stores value 1
let x = 2; // ReferenceError: Cannot access 'x' before initialization

// Re-initialized Identifiers
let x = 1; // Identifier x is initialized with let keyword, stores value 1
let x = 2; // SyntaxError: Identifier 'x' has already been declared

// Arithmetic Operations
let x = 1; // Identifier x is initialized with let keyword, stores value 1
x += 2; // Identifier x performs addition of 1 and 2, stores new value 3

// Block Scope
let x = 1; // Identifier x is initialized with let keyword outside the block scope, stores value 1
{ let x = 2; } // Identifier x is initialized with let keyword within the block scope, stores value 2
console.log(x); // Identifier x is 1, since the let keyword is a block scope

const myIdentifier

// Hoisted Identifiers
x = 1; // Identifier x stores value 1
const x = 2; // ReferenceError: Cannot access 'x' before initialization

// Re-initialized Identifiers
const x = 1; // Identifier x is initialized with const keyword, stores value 1
const x = 2; // SyntaxError: Identifier 'x' has already been declared

// Arithmetic Operations
const x = 1; // Identifier x is initialized with const keyword, stores value 1
x += 2; // TypeError: Assignment to constant variable

// Block Scope
const x = 1; // Identifier x is initialized with const keyword outside the block scope, stores value 1
{ const x = 2; } // Identifier x is initialized with const keyword within the block scope, stores value 2
console.log(x); // Identifier x is 1, since the const keyword is a block scope

function myIdentifier

function myFunction(myArgumentn) {
return null;
}

DATA TYPES

There are 2 categories of JavaScript data types; Primitive Value and Reference Value
Primitive values are data that are stored directly in an identifier (includes numbers, strings, booleans, null, undefined)
Reference values are objects that are stored in memory and accessed through a reference (includes arrays, objects, functions)

Number

JavaScript Numbers are primitive values for storing number that can be written with or without decimals
JavaScript Numbers can also be written with scientific notation (exponent) for large/extra small numbers
Certain calculation will produce keywords like NaN (Not A Number), Infinity (positive infinity), -Infinity (negative infinity)
64-bit Floating Point
JavaScript Numbers does not define different types of numbers, like integers, short, long, floating-point, etc.
JavaScript Numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard
This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63
Integer Precision
Integers (numbers without a period or exponent notation) are accurate up to 15 digits
If the number exceed more than 20 digits, the numbers will be displayed as exponent
let myNumber1 = 999999999999999; // myNumber1 = 999999999999999 (accurate up to 15 digits)
let myNumber2 = 9999999999999999; // myNumber2 = 1000000000000000 (rounded up when exceed more than 15 digits)
let myNumber3 = 999999999999999999999; // myNumber3 = 1e+21 (rounded up when exceed more than 20 digits)
Decimal Point Precision
The maximum number of decimals are accurate up to 16 decimal points
The decimal will be rounded up when the last digit is ≥7
The decimal will be rounded down when the last digit is ≤6
let myNumber1 = 0.9999999999999991; // myNumber1 = 0.9999999999999991 (accurate up to 16 decimal points)
let myNumber2 = 0.99999999999999916; // myNumber2 = 0.9999999999999991 (rounded down when exceed more than 16)
let myNumber3 = 0.99999999999999917; // myNumber3 = 0.9999999999999992 (rounded up when exceed more than 16)
Base Number
By default, JavaScript displays numbers as base 10 decimals
If the numeric constants are preceded by 0x, JavaScript inteprets it as a hexadecimal number
The hexadecimal numbers are NOT case sensitive
let myNumber1 = 0xff; // myNumber1 = 255
let myNumber2 = 0x1E; // myNumber2 = 30
JavaScript Number Methods & Properties
constructor
EPSILON
isFinite()
isInteger()
isNaN()
isSafeInteger()
MAX_SAFE_INTEGER
MIN_SAFE_INTEGER
MAX_VALUE
MIN_VALUE
NaN
NEGATIVE_INFINITY
POSITIVE_INFINITY
parseFloat()
parseInt()
prototype
toExponential(x)
toFixed(x)
toLocaleString()
toPrecision(x)
toString()
valueOf()

String

JavaScript Strings are primitive values for storing text that can be written with either single quote or double quotes in single-line string
let myString1 = 'Hello World!'; // The string is written in single quote
let myString2 = "Hello World!"; // The string is written in double quotes
let myString3 = ""; // Empty string
Quotes inside string
Quotes can be used inside a string, as long as they don't match the quotes surrounding the string
let myString1 = "You're welcome"; // Valid string (The usage of single quote does not affect the structure of string)
let myString2 = "You"re welcome"; // Invalid string (The usage of double quotes does affect the structure of string)
Escape Characters
The backslash escape character (\) turns special characters into string characters
The characters like \b, \f , \n, \r, \t, \v were originally designed to control typewriters, teletypes, and fax machines (They do not make any sense in HTML)
let myString1 = "You\'re welcome"; // myString1 = You're welcome (display single quote)
let myString2 = "You\"re welcome"; // myString2 = You"re welcome (display double quotes)
let myString3 = "Format directory is C:\\documents\\CatPhoto.jpg"; // myString3 = C:\documents\CatPhoto.jpg (display backslash)
let myString4 = "Example1 \b Example2"; // The (\b) characters represent backspace
let myString5 = "Example1 \f Example2"; // The (\f) characters represent form feed
let myString6 = "Example1 \n Example2"; // The (\n) characters represent new line
let myString7 = "Example1 \r Example2"; // The (\r) characters represent carriage return
let myString8 = "Example1 \t Example2"; // The (\t) characters represent horizontal tabulator
let myString9 = "Example1 \v Example2"; // The (\v) characters represent vertical tabulator
Template String
Templates are strings enclosed in backticks
Templates allow single and double quotes inside a string
Templates allow multiline strings
let myString1 = `Arthur's great sword is called "Excalibur"`; // The string is written in backticks
JavaScript String Methods & Properties
at(i)
charAt(i)
charCodeAt(i)
codePointAt(i)
concat(stringn)
constructor
endsWith("string", n)
fromCharCode(coden)
includes("string")
indexOf("string")
lastIndexOf("string")
length
localeCompare("string")
match(regex)
padEnd(n, "string")
padStart(n, "string")
prototype
repeat(n)
replace(regex)
replaceAll("string", "stringnew")
search(regex)
slice(i, n)
split("string", n)
startsWith("string")
substr(i, n)
substring(i, n)
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
trimEnd()
trimStart()
valueOf()

Boolean

JavaScript Boolean

Null

JavaScript Null (Datatype for null is object)

Undefined

JavaScript Undefined

Array

JavaScript Array

Object

JavaScript Object

OPERATORS

Arithmetic Operators

Arithmetic operators perform arithmetic on numbers with either literals or identifiers
There are some cases where data types other than number can performs this operation as well
let number1 = 4; // The datatype is number
let number2 = 10; // The datatype is number
let string1 = "Hello"; // The datatype is string
let string2 = "World"; // The datatype is string
let boolean1 = true; // The datatype is boolean
let boolean2 = false; // The datatype is boolean
Addition (+)
Subtraction (-)
Multiplication (*)
Exponentiation (**)
Division (/)
Modulus (%)
Increment (++)
Decrement (--)

RAW NOTES

myIdentifier.toString()

The toString() method returns any value as a string
The toString() method does not change the original value
let myValue1 = 100;
let myValue2 = "Hello";
let myValue3 = true;
console.log(typeof myValue1); // Number
console.log(typeof myValue2); // String
console.log(typeof myValue3); // Boolean