Page 189 - Web_Application_v2.0_C12_Fb
P. 189
We can declare multiple variables in the same line in the following way:
var firstName = "Priya", age = 22, city = "Delhi";
Declaration
Declaration means creating a variable, essentially giving it a name, so the program knows it exists.
In JavaScript, this is done using var, let, or const. When you declare a variable, it doesn’t necessarily hold a
value yet.
var name; // Declaring a variable named 'name'
var age; // Declaring a variable named 'age'
const PI; // Error: const must be initialized immediately
Notes
Let and var can declare without initialization. const must be initialized during declaration.
Initialization
Initialization means assigning a value to a declared variable for the first time. The equal to (=) sign is used to
assign a value to a variable.
In JavaScript, you can initialize a variable either during the declaration or later on.
var name = "Aryan"; // Declaration + Initialization
var age = 25; // Declaration + Initialization
const PI = 3.14; // Declaration + Initialization
//const must be initialized immediately
The value and data type of a variable can change during the execution of a program and JavaScript takes care
of it automatically.
Variable Naming Conventions
In JavaScript, following proper variable naming conventions is essential for making code clearer, more
readable, and easier to maintain.
Some rules and best practices for naming variables in JavaScript are as follows:
Camel Case Notation: JavaScript variables are typically written in camelCase. The first word starts with a
lowercase letter, and each subsequent word starts with an uppercase letter.
For example:
var firstName= "Rahul";
var studentAge = 18;
Case Sensitivity: JavaScript variable names are case-sensitive. This means firstName, Firstname, and
firstname are all different variables.
For example:
var city = "Delhi";
var City = "Mumbai";
Descriptive Names: Use meaningful names that describe the value the variable holds or the purpose it serves.
For example:
var userScore = 95; // good
var x= 95; // not descriptive
JavaScript Part 2 187

