Javascript
In 1995, a programmer named Brendan Eich was working at Netscape. At that time, websites were mostly static — they could display information, but they couldn’t really interact with users.
Netscape wanted to make web pages more dynamic and engaging. So Brendan Eich was given a task: create a scripting language that would run inside the browser.Incredibly, he built the first version of JavaScript in just 10 days.
What Is JavaScript?
JavaScript is a programming language used to make websites interactive and dynamic.It controls what happens on a webpage. For example, JavaScript makes it possible to:
- Respond when a user clicks a button
- Check if a form is filled out correctly (form validation)
- Show animations and sliders
- Update content on the page without refreshing it
Without JavaScript, websites would feel static and unresponsive.
Flow Chart: How JavaScript Runs in the Browser
Browser loads HTML
↓
Browser sees <script> tag
↓
Browser sends code to JavaScript Engine
↓
Engine executes the code
↓
Result appears on screen
JavaScript variables
In JavaScript, a variable is a named container used to store data. Think of it as a labeled box where you keep information so you can use it later in your program.
Instead of repeating a value multiple times in your code, you store it inside a variable and refer to it by name. This makes your code cleaner, easier to read, and simpler to maintain.
For example:
let userName = "John";
In this case, userName is the variable, and it stores the value "John". Whenever you use userName in your program, JavaScript will use the stored value.
Why Do We Use Variables?
- Store values (numbers, text, objects, etc.)
- Reuse data
- Update values during program execution
How to Declare Variables in JavaScript
JavaScript provides three ways to declare variables:
- let
- const
- var
What is let in JavaScript?
let is a keyword used to declare a variable. It was introduced in ES6 (2015) and is now the recommended way to create variables when the value may change later.
let and const
Both let and const only exist inside the block { } where they are declared.
Example with let
if (true) {
let name = "John";
}
console.log(name); // ❌ Error
Scope vs Reassignment
- Scope → Where the variable can be used
- Reassignment → Whether you can change its value
let can be reassigned and const cannot be reassigned
What is const in JavaScript?
const is used to make a constant variable — a variable that cannot be changed once it has a value.
Example with const
if (true) {
const age = 25;
}
console.log(age); // ❌ Error
What is var in JavaScript?
var is a way to declare a variable in JavaScript. It has been in JavaScript since the very beginning (ES5 and earlier), but now it’s mostly replaced by let and const in modern code.
A var variable belongs to the function it is declared in, not to a block scope { }.You can change its value anytime.
Top comments (0)