Next Topic ⮕
Set Up JavaScript EnvironmentWhat is JavaScript?
Introduction
JavaScript is more than just a scripting language — it’s the heartbeat of dynamic, interactive web experiences. From validating forms to creating immersive games and single-page applications, JavaScript is the bridge between static content and real-time user interaction.
What is JavaScript?
JavaScript is a high-level, interpreted programming language that runs in the browser. It allows developers to add behavior to websites, making them interactive and engaging. Originally created by Netscape in 1995, JavaScript has since become a cornerstone of web development, alongside HTML and CSS.
Why Use JavaScript?
- To manipulate HTML and CSS dynamically (DOM manipulation)
- To handle user inputs and respond to events
- To fetch data from servers without reloading pages (AJAX)
- To validate form inputs before submitting to the server
- To build full-fledged front-end applications using frameworks like React, Angular, or Vue
JavaScript in Action – A Simple Example
document.getElementById("demo").innerHTML = "Hello, JavaScript!";
HTML Setup:
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello, JavaScript!";
</script>
Hello, JavaScript!
How JavaScript Works
JavaScript is executed by the browser's JavaScript engine. It follows a single-threaded, event-driven model, meaning it can run asynchronously without blocking the main thread.
Variables and Data Types
Variables can be declared using let
, const
, or var
. Data types include:
- String – Text like "Hello"
- Number – 42, 3.14
- Boolean – true or false
- Array – A list: [1, 2, 3]
- Object – A key-value pair: { name: "John" }
let name = "Alice";
let age = 30;
let isStudent = false;
let scores = [90, 85, 88];
let user = { name: "Alice", age: 30 };
Functions
Functions are blocks of reusable code that perform specific tasks.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Sam"));
Hello, Sam!
Conditionals and Loops
Control flow helps JavaScript make decisions and repeat tasks.
// Conditional
let score = 80;
if (score >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
// Loop
for (let i = 0; i < 3; i++) {
console.log("Iteration", i);
}
Pass
Iteration 0
Iteration 1
Iteration 2
Events in JavaScript
JavaScript responds to user actions using events.
<button onclick="alert('Button clicked!')">Click Me</button>
DOM Manipulation
JavaScript can access and modify the entire structure of a web page using the Document Object Model (DOM).
document.querySelector("h1").style.color = "blue";
Advanced Topics (Brief Overview)
1. ES6+ Features
let
andconst
for better variable scoping- Arrow functions:
const add = (a, b) => a + b;
- Template literals:
`Hello, ${name}`
- Destructuring and spread/rest operators
2. Asynchronous JavaScript
Using callbacks, promises, and async/await
to handle asynchronous tasks.
async function fetchData() {
let response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
let data = await response.json();
console.log(data);
}
fetchData();
{ userId: 1, id: 1, title: "delectus aut autem", completed: false }
3. Object-Oriented Programming (OOP)
class Person {
constructor(name) {
this.name = name;
}
greet() {
return "Hi, I'm " + this.name;
}
}
let p = new Person("Leo");
console.log(p.greet());
Hi, I'm Leo
Conclusion
JavaScript is the soul of modern web development. Whether you're validating forms, creating animations, or building complex applications, JavaScript gives you the power to craft seamless user experiences. Start small, experiment often, and gradually dive deeper — because every master coder once typed console.log("Hello, world")
for the first time.
Next Topic ⮕
Set Up JavaScript Environment