What is JavaScript?
JavaScript (JS) is a versatile, high-level, interpreted programming language that is one of the core technologies of the World Wide Web. Alongside HTML and CSS, JavaScript provides interactivity and dynamic behavior to web pages.
It's used for:
- Making web pages interactive (e.g., animations, pop-ups, form validation)
- Manipulating HTML and CSS dynamically
- Fetching data from servers without reloading the page (AJAX)
- Building complex web applications and even mobile apps (React Native, NativeScript)
- Server-side development (Node.js)
Where Does JavaScript Live?
JavaScript code can be included in an HTML document in two main ways:
- Inline: Directly within HTML tags using event attributes like
onclick
,onmouseover
, etc. (Generally discouraged for larger scripts). - Internal: Within a
<script>
tag in the<head>
or<body>
of the HTML document. - External: In a separate
.js
file, linked to the HTML document using thesrc
attribute of the<script>
tag. This is the most recommended approach for organization and reusability.
Example: Internal JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<title>JS Example</title>
</head>
<body>
<h1>Hello from HTML!</h1>
<button onclick="changeText()">Click Me</button>
<p id="myParagraph">This text will change.</p>
<script>
function changeText() {
document.getElementById("myParagraph").innerText = "JavaScript made this happen!";
alert("Button clicked!");
}
</script>
</body>
</html>
Example: External JavaScript
Create a file named script.js
with the following content:
// script.js
function changeText() {
document.getElementById("myParagraph").innerText = "JavaScript made this happen!";
alert("Button clicked!");
}
Link it in your HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>JS Example</title>
</head>
<body>
<h1>Hello from HTML!</h1>
<button onclick="changeText()">Click Me</button>
<p id="myParagraph">This text will change.</p>
<script src="script.js"></script>
</body>
</html>
Basic JavaScript Concepts
Variables
Variables are used to store data. In JavaScript, you can declare variables using var
, let
, or const
.
var
: Declared variables are globally scoped or function scoped.let
: Declared variables are block-scoped and can be reassigned.const
: Declared variables are block-scoped and cannot be reassigned (constants).
let message = "Hello, World!";
const year = 2023;
var count = 5; // Older way, 'let' is preferred for mutable variables
console.log(message); // Output: Hello, World!
console.log(year); // Output: 2023
// year = 2024; // This would cause an error
Data Types
JavaScript has several built-in data types:
- String: Text (e.g.,
"Hello"
) - Number: Integers and floating-point numbers (e.g.,
10
,3.14
) - Boolean:
true
orfalse
- Object: A collection of properties (key-value pairs)
- Array: An ordered list of values
- Null: Represents the intentional absence of any object value
- Undefined: Indicates that a variable has not been assigned a value
Operators
Operators perform operations on variables and values.
- Arithmetic:
+
,-
,*
,/
,%
(modulo) - Assignment:
=
,+=
,-=
, etc. - Comparison:
==
,===
(strict equality),!=
,!==
,>
,<
,>=
,<=
- Logical:
&&
(AND),||
(OR),!
(NOT)
Functions
Functions are blocks of reusable code designed to perform a particular task.
function greet(name) {
return "Hello, " + name + "!";
}
let greeting = greet("Alice");
console.log(greeting); // Output: Hello, Alice!
Interacting with the DOM
The Document Object Model (DOM) is a programming interface for web documents. It represents the page structure as a tree of objects, allowing JavaScript to dynamically change content, style, and structure.
Try this interactive example:
// This code would go in a script.js file or inside <script> tags
document.getElementById("myButton").addEventListener("click", function() {
const outputDiv = document.getElementById("outputArea");
outputDiv.innerText = "The DOM was updated by JavaScript!";
outputDiv.style.color = "var(--secondary-color)";
outputDiv.style.borderLeft = "4px solid var(--primary-color)";
outputDiv.style.fontStyle = "normal";
});
Key DOM Methods:
document.getElementById('id')
: Selects an element by its ID.document.querySelector('selector')
: Selects the first element matching a CSS selector.element.innerText = '...'
: Sets the text content of an element.element.innerHTML = '...'
: Sets the HTML content of an element.element.style.property = '...'
: Modifies the inline style of an element.element.addEventListener('event', function)
: Attaches an event listener to an element.
Next Steps
This is just a brief introduction. To master JavaScript, you should explore topics like:
- Control Flow (if/else, loops)
- Arrays and Objects in depth
- Asynchronous JavaScript (Promises, async/await)
- ES6+ features (Arrow functions, template literals, destructuring)
- Frameworks and Libraries (React, Vue, Angular)