What is javascript?

JavaScript is a high-level scripting language mainly used for building interactive web pages. It runs in web browsers (like Chrome or Firefox) using an embedded engin (like V8 in Chrome).
It can also run outside browsers using Node.js.

How does javascript run?

Not like C, JavaScript cannot be directly compiled into a binary file, then executed by operating system.

Here I will clarify this layer by layer, from JS code to the Hardware.

JavaScript Engine

The execution of JavaScript need JavaScript Engine (Like V8, SpiderMoney, or JavaScriptCore).

Engine is a program, the core program that reads and executes JavaScript. It has several layers inside:

  1. Parser
    Converts your code into tokens, then into an AST (Abstract Syntax Tree) - a structured representation of the code.

  2. Interpreter (Ignition in V8)
    Reads the AST and converts it to bytecode.
    Starts executing the code immediately - fast startup.

  3. Compiler (TurboFan in V8)
    While the interpreter runs, the engine detects hot code (frequently used).
    It compiles this hot code into optimized machine code for faster performance.

  • Summary:
    JavaScript is both interpreted and just-in-time (JIT) compiled.

APIs / Runtime Environment

JavaScript can’t access hardware directly like C. It relies on the environment:

  • In the Browser:

    • provides Web APIs like document, window, fetch, setTimeout, etc.
    • Your code runs in the engine (e.g., V8 in Chrome), and calls these APIs.
  • In Node.js:

    • provides Node APIs like fs (file system), http, etc.
    • Still uses V8 engine, but not te browser APIs.

Operating System

Whether browser or Node.js, both eventually use syscalls to talk to the OS:

  • reading files, opening sockets, allocating memory, etc.

But in JavaScript, you cont see this directly - it’s abstracted behind APIs.

Hardware (CPU, Memory)

The compiled machine code finally runs on the CPU, using system resources.

Variables

Before the ES-6, we use keyword var to define variables.
But after the ES-6 is published, we use let to define a variable.

1
2
3
let name = 'Mosh';
let firstName = 'Mosh', lastName = 'Hamedani';
console.log(name);

Notice

  • Variable’s name cannot be a reserved keyword (like let, if, etc.).
  • Variable’s name cannot start with a number (like 1name).
  • Variable’s name cannot contain a space of hyphen (-).
  • Variable’s name is case-sensitive (name and Name is different).
  • Variable’s name should be meaningful.

Constants

We use keyword const to define a constant.

1
2
const interestRate = 0.3;
console.log(interestRate);

Data Types

In JavaScript, data types are divided into two main categories:

Primitives (Value Types)

Primitive types are values that could be assigned to a variable.
These are simple, immutable values. When you assign them to a variable, you’re storing the actual value.

  • String
  • Number
  • Boolean
  • undefined
  • null

Example:

1
2
3
4
5
let name = 'Mosh'; // String Literal
let age = 30; // Number Literal
let isApproved = false; // Boolean Literal
let firstName = undefined; // Undefined
let selectedColor = null; // Null

Reference Types

These are objects or complex structures. When assigned to a variable, what gets stored is a reference (memory address) to the value in memory, not the value itself.

  • Object
  • Array
  • Function
  • Date
  • RegExp

Example:

1
let person = { name: "Alice" }; // Person stores a reference to the object

Dynamic Typing

One thing that separates JavaScript from a lot of programming languages is that JavaScript is a dynamic language, whose variable could be changed from one type to another type.

1
2
let x = 10; // x is a number
x = "hello"; // Now x is a string

As example, Variables can change type at any time.

Type Coercion

JavaScript will automatically convert types in some cases - this is called type coercion.

1
2
let a = '5' + 1; // "51" (Number 1 is converted to string)
let b = 5 + true; // 6 (True is converted to number 1)

Equality Check

Because of Dynamic Typing, variables may be equal, even though they are in different type.

1
2
"5" == 5 // true (Type coercion happens)
"5" === 5 // false (This called strict equality, no coercion)

Manually Type Conversion

You can also convert types manually:

1
2
3
4
String(123); // "123"
Number("456"); // 456
Boolean(0); // false
parseInt("42px"); // 42

Object

An object in JavaScript is like an object in real life.
When we are dealing with multiple related variables, we can put these variables inside of an object.
The variables inside of an object called property.

1
2
3
4
5
6
let person = {
name: 'Mosh',
age: 30
};

console.log(person); // {name: "Mosh", age: 30}

We could use dot notation and bracket notation to visit the property of an object.

1
2
3
4
person.name = 'John'; // Dot notation

let selection = 'name';
console.log(person[selection]); // John

Array

Arrays in JavaScript is very similar to lists in python, but there are also some differences.

1
2
3
4
5
6
7
let selectedColors = ['red', 'blue'];

console.log(selectedColors); // ["red", "blue"]
console.log(selectedColors[0]); // red

selectedColors[2] = 1;
console.log(selectedColors); // ["red", "blue", 1]

Compared to List in Python

Similarities

  • They are both ordered collection
  • They can hold mixed types
  • Both have dynamic size
  • Indexed by position
  • Mutable

Differences

  • JavaScript Array doesn’t support negative indexes (arr[-1] -> undefined), while Python List does (lst[-1] -> last item).
  • JavaScript Array actually is an array-like object, but Python List is just a List.
1
2
typeof selectedColors; // "object"
console.log(selectedColors.length); // 3

Functions

In JavaScript, We define a function with the keyword function.

1
2
3
4
5
function greet(name) {
console.log('Hello ' + name);
}

greet('John'); // Hello John

Types of Functions

1
2
3
4
5
6
function square(number) {
return number * number;
}

let number = square(2);
console.log(number);