javascript
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:
Parser
Converts your code into tokens, then into an AST (Abstract Syntax Tree) - a structured representation of the code.Interpreter (Ignition in V8)
Reads the AST and converts it to bytecode.
Starts executing the code immediately - fast startup.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.
- provides Web APIs like
In Node.js:
- provides Node APIs like
fs
(file system), http, etc. - Still uses V8 engine, but not te browser APIs.
- provides Node APIs like
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 | let name = 'Mosh'; |
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 | const interestRate = 0.3; |
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 | let name = 'Mosh'; // String Literal |
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 | let x = 10; // x is a number |
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 | let a = '5' + 1; // "51" (Number 1 is converted to string) |
Equality Check
Because of Dynamic Typing, variables may be equal, even though they are in different type.
1 | "5" == 5 // true (Type coercion happens) |
Manually Type Conversion
You can also convert types manually:
1 | String(123); // "123" |
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 anobject
.
The variables inside of anobject
calledproperty
.
1 | let person = { |
We could use dot notation and bracket notation to visit the property of an object.
1 | person.name = 'John'; // Dot notation |
Array
Arrays in JavaScript is very similar to lists in python, but there are also some differences.
1 | let selectedColors = ['red', 'blue']; |
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 | typeof selectedColors; // "object" |
Functions
In JavaScript, We define a function with the keyword
function
.
1 | function greet(name) { |
Types of Functions
1 | function square(number) { |