JavaScript (JS) Fundamentals: The Heart of Dynamic Web
JavaScript (JS) is a high-level, interpreted programming language primarily known for bringing web pages to life. Contrary to its name, it has little connection to Java; it was initially created by Brendan Eich in 1995 at Netscape.
Today, JS is no longer confined to the browser. Thanks to runtime environments like Node.js, it is used for server-side (backend) development, mobile apps, and even desktop applications, making it one of the most versatile languages globally.
Role in Web Development
In web development, JavaScript is the third pillar, working in conjunction with:
-
HTML (HyperText Markup Language): Defines the structure and content of the page.
-
CSS (Cascading Style Sheets): Manages the style and presentation (colors, fonts, layout).
-
JavaScript: Adds interactivity and dynamic behavior (animations, form validation, updating content without reloading the page).
Core Concepts in JavaScript
1. Variables and Data Types
In JS, variables can be declared with var (old method), let (block-scoped, reassignable), or const (constant, not reassignable).
JavaScript is a weakly and dynamically typed language. The main primitive data types are:
| Data Type | Description | Example |
String |
Text. | 'Hello World', "JS" |
Number |
Numbers (integers and floats). | 42, 3.14 |
Boolean |
Truth values. | true, false |
null and undefined |
Intentional absence of value (null) or declared but unassigned variable (undefined). |
2. Operators
-
Arithmetic Operators:
+,-,*,/,%(modulo). -
Comparison Operators:
==(loose equality),===(strict equality in value AND type),!=,!==,>,<. -
Logical Operators:
&&(AND),||(OR),!(NOT).
3. Control Flow
Control structures are similar to those in other languages:
-
Conditions:
if,else if,else. -
Loops:
for,while, andfor...of(to iterate over array elements) orfor...in(to iterate over object properties).
JavaScript
// Condition example
let age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
4. Functions
Functions are crucial for organizing code. They can be defined classically or via the « Arrow Function » syntax (introduced in ES6).
JavaScript
// Classical function declaration
function greet(name) {
return "Hello, " + name;
}
// Arrow Function
const multiply = (a, b) => a * b;
5. Objects and Arrays
-
Objects: Objects are collections of properties. This is the fundamental structure for modeling complex data in JS.
JavaScript
const user = { name: "Bob", email: "bob@mail.com", active: true }; -
Arrays: These are ordered lists of elements.
JavaScript
const colors = ["red", "green", "blue"]; console.log(colors[0]); // Displays "red"
Modern JavaScript (ES6+)
Since 2015 (with the release of ES6 or ECMAScript 2015), JavaScript has evolved significantly, introducing crucial concepts:
-
Modules: For importing and exporting features between different files.
-
Classes: For object-oriented programming.
-
Promises and Async/Await: To handle asynchronous code (like waiting for data from a server) in a clearer, structured way.
JavaScript’s power lies in its ability to manipulate the DOM (Document Object Model) of web pages, enabling rich interactions and a smooth user experience.