The Programming Language of the Web
JavaScript is a high-level, interpreted programming language that conforms to the ECMAScript specification. It is a language that is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.
Alongside HTML and CSS, JavaScript is one of the core technologies of the World Wide Web. JavaScript enables interactive web pages and is an essential part of web applications. The vast majority of websites use it for client-side page behavior, and all major web browsers have a dedicated JavaScript engine to execute it.
Runs directly in the browser without needing constant communication with the server
Supports object-oriented programming with prototype-based inheritance
Responds to user interactions like clicks, form submissions, and keyboard events
Can interact with browser APIs and third-party services
JavaScript includes standard programming features like variables, data types, operators, control structures, functions, and objects. Here's a simple example:
// Variables and data types
let message = "Hello, World!"; // String
const score = 100; // Number
let isActive = true; // Boolean
// Function definition
function greet(name) {
return `Hello, ${name}!`;
}
// Function call
const greeting = greet("Alice");
console.log(greeting); // Output: Hello, Alice!
// Conditional statement
if (score > 50) {
console.log("You passed!");
} else {
console.log("Try again!");
}
Use let
, const
, or var
to store and manage data
Reusable blocks of code that perform specific tasks
Conditionals and loops to control program execution
One of JavaScript's most powerful features is its ability to manipulate the Document Object Model (DOM), which represents the structure of an HTML document:
// Selecting elements
const button = document.querySelector('#myButton');
const items = document.querySelectorAll('.item');
// Adding event listeners
button.addEventListener('click', function() {
// Modify CSS styles
this.style.backgroundColor = 'blue';
// Change text content
this.textContent = 'Clicked!';
// Add/remove classes
this.classList.add('active');
});
// Creating new elements
const newDiv = document.createElement('div');
newDiv.textContent = 'New element';
document.body.appendChild(newDiv);
JavaScript has a rich ecosystem of frameworks and libraries that extend its capabilities for building complex applications:
A declarative, efficient, and flexible JavaScript library for building user interfaces
A platform and framework for building single-page client applications using HTML and TypeScript
A progressive framework for building user interfaces that is designed to be incrementally adoptable
A JavaScript runtime built on Chrome's V8 JavaScript engine for building server-side applications
A minimal and flexible Node.js web application framework that provides a robust set of features
A fast, small, and feature-rich JavaScript library that simplifies HTML document traversal and manipulation
Originally developed by Brendan Eich at Netscape under the name Mocha, then LiveScript, finally JavaScript.
Added regular expressions, better string handling, try/catch exception handling, and more.
Added strict mode, JSON support, functional array methods, and property descriptors.
Major update with classes, modules, arrow functions, promises, and many new syntax features.
Annual releases with incremental improvements including async/await, optional chaining, and nullish coalescing.
JavaScript can be added to HTML documents in several ways:
<!-- Internal JavaScript -->
<script>
console.log("Hello from internal JavaScript!");
</script>
<!-- External JavaScript -->
<script src="script.js"></script>
<!-- Inline JavaScript -->
<button onclick="alert('Button clicked!')">Click Me</button>