var, let, and const?
Coding with JavaScript. Learn variables, data types, operators, functions, conditionals, and DOM manipulation with practical examples and mini labs.
JavaScript (JS) makes web pages interactive. With JS you can read user input, react to clicks, change the page without reloading, and talk to servers. In this lecture you’ll master variables, data types, operators, functions, conditionals, and DOM manipulation.
Use let and const (modern, block-scoped). Avoid var for now.
js
const siteName = "ElecturesAI"; // constant
let visitors = 0; // can change later
visitors += 1;
When to use which?
const by default;let when you must reassign.string, number, boolean, null, undefined, bigint, symbolobject, array, function, Date, etc.js
const title = "JS Intro"; // string
const price = 499.99; // number
const isPublished = true; // boolean
let notSet; // undefined
const tags = ["js","basics"]; // array (object)
const author = {name:"NexAI"}; // object
+ - * / % **= += -==== !== < > <= >=&& || !+ or template literal `Hi ${name}`js
const a = 7, b = 3;
const sum = a + b; // 10
const both = a > 5 && b < 5 // true
const msg = `Sum is ${sum}` // "Sum is 10"

Two common styles:
// Function declaration
function greet(name) {
return `Hello, ${name}`;
}
// Arrow function
const square = n => n * n;
console.log(greet("Student"));
console.log(square(5)); // 25
Parameters & default values
function priceAfterDiscount(price, off = 0.1) {
return price * (1 - off);
}
Conditionals
const score = 78;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else {
console.log("Keep going!");
}
// Ternary (short form)
const statusText = score >= 50 ? "Pass" : "Fail";
Common pitfalls
=== (strict equality) instead of == to avoid type coercion surprises.(isLoggedIn && isAdmin).The DOM (Document Object Model) lets JS read/change HTML & CSS at runtime.
<h1 id="title">Hello</h1>
<button class="btn">Change Text</button>
const title = document.getElementById("title");
const button = document.querySelector(".btn");
Changing Content & Styles
title.textContent = "Welcome to JavaScript";
title.style.color = "#2f855a";
Responding to Events
button.addEventListener("click", () => {
title.textContent = "You clicked the button!";
});
Creating & Appending Elements
const list = document.createElement("ul");
["HTML","CSS","JS"].forEach(item => {
const li = document.createElement("li");
li.textContent = item;
list.appendChild(li);
});
document.body.appendChild(list);
HTML
<h2 id="count">0</h2>
<button id="inc">+</button>
<button id="dec">-</button>
<button id="reset">Reset</button>
CSS
h2 { font: 700 2rem/1.1 system-ui; color:#2f855a; }
button { margin:.25rem; padding:.5rem 1rem; }
JavaScript
const countEl = document.getElementById("count");
let count = 0;
const render = () => (countEl.textContent = count);
document.getElementById("inc").addEventListener("click", () => { count++; render(); });
document.getElementById("dec").addEventListener("click", () => { count--; render(); });
document.getElementById("reset").addEventListener("click", () => { count = 0; render(); });
render();
The approach followed at E Lectures reflects both academic depth and easy-to-understand explanations.
const/let, know your data types and operators.var: function-scoped, hoisted (legacy).
let: block-scoped, reassignable.
const: block-scoped, not reassignable (but object contents can change).
Use === (strict) to avoid type coercion surprises.
0 == "0" // true
0 === "0" // false
undefined: variable declared but not assigned.
null: an intentional empty value.
typeof 42; // "number"
Array.isArray([]); // true
Primitives: stored by value (string, number, boolean, null, undefined, bigint, symbol).
Reference: objects/arrays/functions stored by reference.
Declarations are moved to the top at compile time. var becomes undefined; let/const exist in a temporal dead zone until initialized.
Arrow functions are shorter, don’t have their own this/arguments.
const add = (a,b)=>a+b; // arrow
function add2(a,b){return a+b;} // declaration