The programming language for interactive web content
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.
JavaScript syntax is the set of rules that define how JavaScript programs are constructed:
// Variables
let message = "Hello, World!";
const pi = 3.14159;
// Functions
function greet(name) {
return `Hello, ${name}!`;
}
// Output
console.log(greet("Developer"));
Add JavaScript directly to HTML elements using event attributes:
<button onclick="alert('Hello World!')">
Click Me
</button>
Note: Inline JavaScript is not recommended for complex code as it mixes structure with behavior.
Place JavaScript code inside a <script> element in the HTML document:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
function showAlert() {
alert('Hello from internal JavaScript!');
}
</script>
</head>
<body>
<button onclick="showAlert()">Click Me</button>
</body>
</html>
Link to an external JavaScript file (recommended approach):
// In your HTML file
<head>
<script src="script.js" defer></script>
</head>
// In script.js
function showAlert() {
alert('Hello from external JavaScript!');
}
// Wait for DOM to load
document.addEventListener('DOMContentLoaded', () => {
const button = document.querySelector('button');
button.addEventListener('click', showAlert);
});
External JavaScript is the most maintainable approach as it separates behavior from structure.
Master these fundamental JavaScript concepts to build interactive web applications:
JavaScript has dynamic types and seven fundamental data types:
| Type | Example | Description |
|---|---|---|
| Number | 42, 3.14 | Integer and floating point numbers |
| String | "Hello" | Textual data enclosed in quotes |
| Boolean | true, false | Logical true/false values |
| Null | null | Intentional absence of any value |
| Undefined | undefined | Variable that has not been assigned a value |
| Object | {name: "John"} | Collection of key-value pairs |
| Symbol | Symbol('id') | Unique and immutable primitive value |
Try these examples to see JavaScript in action:
Click the buttons to change this text.
// JavaScript code for the demo
function changeText() {
const element = document.getElementById('demo-element');
element.innerHTML = 'Text changed with JavaScript!';
}
function changeColor() {
const element = document.getElementById('demo-element');
element.style.color = '#f7df1e';
element.style.backgroundColor = '#2c2c2c';
}
The browser console is a powerful tool for testing JavaScript code and debugging:
To open the console:
ECMAScript 2015 (ES6) introduced many new features that modernized JavaScript:
// Arrow functions
const add = (a, b) => a + b;
// Template literals
const name = "Sarah";
const greeting = `Hello, ${name}!`;
// Destructuring
const person = { firstName: 'John', age: 30 };
const { firstName, age } = person;
// Spread operator
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
// Modules
import { functionName } from './module.js';
export const myVariable = 42;
Modern JavaScript tutorial from the basics to advanced topics.
Visit JavaScript.infoWell-regarded book available free online with interactive examples.
Read the Book