Complete guide to JavaScript variables covering declaration initialization scope and control statements including conditional statements switch statements and loops for dynamic programming
This document explains JavaScript variables, their declaration, initialization, and scope, as well as control statements like conditional statements, switch statements, and loops. It highlights how these features enable dynamic program flow and efficient data handling in JavaScript.
declared using the var keyword followed by the variable name, e.g., var age.var age = 54, or assigned a value later.loosely typed, so variables take their data type from the assigned value and can change type during execution.undefined._), or dollar sign ($).var keyword are accessible throughout the program.1var name = 'John'
2var age = 30
3console.log(name + ' is ' + age + ' years old.')
1if (age > 18) {
2 console.log('Adult')
3} else {
4 console.log('Minor')
5}
1for (var i = 0; i < 5; i++) {
2 console.log('Iteration ' + i)
3}
JavaScript variables allow dynamic data handling with flexible typing and scoping rules.
var keyword followed by the variable name. They can be initialized during declaration, e.g., var age = 30, or assigned a value later._), or dollar sign ($). Subsequent characters can include letters, digits (0-9), underscores, or dollar signs.var keyword automatically have global scope, making them accessible throughout the program.undefined.if, switch, and loops direct the program flow, enabling conditional execution and repetitive tasks.for loop is used when you need to execute a block of code a specific number of times, such as iterating over a range of numbers.age and Age are treated as distinct.