- Understand JavaScript and its role in web development
- Declare and use variables properly
- Work with primitive and complex data types
- Use operators in expressions
- Understand variable scope
- Practice JavaScript fundamentals
BMC201 - Web Technology
2026-02-12
Lecture 12
JavaScript Variables & Data Types
Week 3 | Unit II: CSS Styling & JavaScript
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor
Learning Objectives
JavaScript: The Language of the Web
Remember:
HTML = Structure
CSS = Styling
JS = Behavior & Interactivity
Including JavaScript in HTML
Inline (not recommended):
Internal script tag:
External file (best practice):
Always put <script> at end of <body> tag
Declaring Variables
Variables store data. Declare with let, const, or var:
let name = "Alice"; /* Can be reassigned */
const age = 25; /* Cannot be reassigned */
var city = "New York"; /* Older way (avoid) */Best practices: - Use const by default - Use let if value changes - Avoid var (old syntax)
Variable Naming Rules
_, or $_, $name vs Name)firstName, myVariablelet, const, if, etc.Good examples:
Bad examples:
Primitive Data Types
Basic types that hold single values:
More Primitive Types
Arrays (Complex Type)
Ordered collection of values:
const fruits = ["apple", "banana", "orange"];
const numbers = [1, 2, 3, 4, 5];
const mixed = [1, "hello", true, null];
console.log(fruits[0]); /* "apple" */
console.log(fruits.length); /* 3 */Array methods:
Objects (Complex Type)
Key-value pairs:
const person = {
name: "Alice",
age: 25,
city: "New York",
isStudent: true
};
console.log(person.name); /* "Alice" */
console.log(person["age"]); /* 25 */
person.age = 26; /* Modify property */Objects are key in JavaScript programming!
JavaScript Operators
Logical Operators
Combine boolean values:
true && false /* AND - both must be true */
true || false /* OR - at least one true */
!true /* NOT - reverses boolean */Practical example:
Type Conversion & Coercion
Converting between types:
String(25) /* "25" - to string */
Number("25") /* 25 - to number */
Boolean(1) /* true - to boolean */
parseInt("25") /* 25 - string to integer */
parseFloat("25.99") /* 25.99 - string to float */Automatic coercion (watch out!):
Working with Strings
const text = "Hello World";
text.length /* 11 */
text.toUpperCase() /* "HELLO WORLD" */
text.toLowerCase() /* "hello world" */
text.substring(0, 5) /* "Hello" */
text.includes("World") /* true */
text.replace("World", "JS") /* "Hello JS" */String concatenation:
Variable Scope
Where variables are accessible:
const global = "accessible everywhere";
function myFunction() {
const local = "only inside function";
console.log(global); /* Works */
}
console.log(local); /* Error - not defined outside */Block scope:
Console Methods for Debugging
console.log("Regular message"); /* Basic output */
console.error("Error message"); /* Red error text */
console.warn("Warning message"); /* Yellow warning */
console.table([1, 2, 3]); /* Table format */
console.clear(); /* Clear console */Debugging tip:
Practical JavaScript Example
// Declare variables
const student = {
name: "Alice",
grades: [85, 90, 78, 92]
};
// Calculate average
let sum = student.grades[0] + student.grades[1] +
student.grades[2] + student.grades[3];
let average = sum / student.grades.length;
// Output result
console.log(`${student.name}'s average: ${average.toFixed(1)}`);
/* Output: Alice's average: 86.3 */Live Classroom Demo: Variables & Data Types
Use this interactive page during class to demonstrate:
let, const, and value updatestypeofNumber() and String()== vs === comparison behaviorAlternative site path: /html/demos/lecture-12-demo.html
Resources & References
Questions?
Next: Week 4 - JavaScript Functions & Events