The 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.
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.
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)
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.
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"
JavaScript automatically manages memory by cleaning up unused data through garbage collection algorithms.
Example:
let obj = { name: "John" };
obj = null; // Memory is automatically freed
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
JavaScript runs via engines embedded in browsers and environments like Node.js.