The JavaScript
Basics of JavaScript
Basics of JavaScript
JavaScript (JS) is a high-level, dynamic, and interpreted programming language primarily used for web development. It was initially created by Brendan Eich in 1995 for the Netscape Navigator browser. Today, JavaScript is widely used in both front-end and back-end programming, thanks to Node.js and various frameworks.
What Kind of Programming Language is JavaScript?
JavaScript is classified as an interpreted language, meaning it does not require prior compilation, unlike languages such as C or Java.
It is a multi-paradigm language, supporting various programming styles, including object-oriented programming (OOP), functional programming, and event-driven programming.
JavaScript is a client-server language, meaning it can run both in browsers and on servers.
Core Principles of JavaScript
1. Single-threaded and Asynchronous Nature
JavaScript operates on a single thread, meaning it executes only one operation at a time. However, it supports asynchronous capabilities using callbacks, Promises, and async/await mechanisms to maintain non-blocking execution flow.
Example: Using setTimeout
console.log("Start");
setTimeout(() => console.log("Timeout"), 2000);
console.log("End");
// Output: Start → End → Timeout (after 2 seconds)
2. Dynamic Typing
In JavaScript, variables do not have fixed data types, allowing easy changes to data types.
Example:
let x = 10; // x is a number
x = "Hello"; // x is now a string
This feature can lead to unexpected errors, which is why tools like TypeScript and static typing are used in JavaScript.
3. Prototype-Based Inheritance
In JavaScript, objects inherit properties and methods from other objects using the prototype mechanism.
Example: Prototype inheritance
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log("Hello, my name is " + this.name);
};
let person1 = new Person("Aram");
person1.sayHello(); // "Hello, my name is Aram"
4. Garbage Collection
JavaScript automatically manages memory by cleaning up unused data through garbage collection algorithms.
Example:
let obj = { name: "John" };
obj = null; // Memory is automatically freed
5. Event Loop
JavaScript's Event Loop mechanism manages asynchronous operations, allowing code execution without blocking the program.
Example: Event Loop
console.log("Start");
setTimeout(() => console.log("Timeout"), 0);
console.log("End");
// Output: Start → End → Timeout
How JavaScript Works
JavaScript runs via engines embedded in browsers and environments like Node.js.
- Google Chrome – V8 Engine (also used in Node.js)
- Mozilla Firefox – SpiderMonkey
- Microsoft Edge – Chakra
Example: V8 Engine Workflow
- JavaScript code is first converted into an Abstract Syntax Tree (AST).
- The AST undergoes JIT (Just-In-Time) compilation to produce optimized bytecode.
- V8 executes the bytecode and manages memory.
Key Use Cases for JavaScript
- Web Development (Front-end) – React, Vue.js, Angular
- Server-Side Development (Back-end) – Node.js
- Mobile Apps – React Native, Ionic
- Game Development – Phaser.js, Three.js
- Machine Learning – TensorFlow.js
- 0
- 19