Browse Courses

Javascript Variables

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.


JavaScript Variables

Declaration and Initialization

  • Variables are declared using the var keyword followed by the variable name, e.g., var age.
  • Variables can be initialized during declaration, e.g., var age = 54, or assigned a value later.
  • JavaScript is loosely typed, so variables take their data type from the assigned value and can change type during execution.
  • Uninitialized variables have a value of undefined.

Rules for Variable Names

  • Must start with a letter, underscore (_), or dollar sign ($).
  • Subsequent characters can include letters, digits (0-9), underscores, or dollar signs.
  • Variable names are case-sensitive.

Scope of Variables

  • Local Scope: Variables declared within a function are accessible only inside that function.
  • Global Scope: Variables declared outside a function or without the var keyword are accessible throughout the program.

Example: Variable Declaration and Control Statements

Variable Declaration

1var name = 'John'
2var age = 30
3console.log(name + ' is ' + age + ' years old.')

If Statement

1if (age > 18) {
2  console.log('Adult')
3} else {
4  console.log('Minor')
5}

For Loop

1for (var i = 0; i < 5; i++) {
2  console.log('Iteration ' + i)
3}

Conclusion

JavaScript variables allow dynamic data handling with flexible typing and scoping rules.


FAQ

Variables in JavaScript are declared using the var keyword followed by the variable name. They can be initialized during declaration, e.g., var age = 30, or assigned a value later.

JavaScript is considered loosely typed because variables take their data type from the assigned value and can change type during execution.

JavaScript variable names must start with a letter, underscore (_), or dollar sign ($). Subsequent characters can include letters, digits (0-9), underscores, or dollar signs.

Yes, variables declared without the var keyword automatically have global scope, making them accessible throughout the program.

  • Variables with local scope are accessible only within the function where they are declared.
  • Variables with global scope are accessible throughout the program.

If a variable is declared but not initialized, it has a default value of undefined.

Control statements like if, switch, and loops direct the program flow, enabling conditional execution and repetitive tasks.

A 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.

Yes, JavaScript is case-sensitive, so variable names like age and Age are treated as distinct.

  • Local variables are declared within a function and are accessible only inside that function.
  • Global variables are declared outside any function and are accessible throughout the program.