advertisement
Variables in JavaScript
A variable refers to a symbolic name that holds a value, which keeps changing. For example, the age of a student and the salary of an employee can be treated as variables. A real-life example of variables includes the variables used in algebraic expressions that store values.
In JavaScript, a variable is a unique location in the computer's memory that stores a value and has a unique name. The name of the variable is used to access and read the value stored in it. A variable can store different types of data such as a character, a number, or a string. Therefore, a variable acts as a container for saving and changing values during the execution of the script.
1. Declaring Variables
Declaring a variable refers to creating a variable by specifying the variable name. For example, you can create a variable named studName to store the name of a student. Here, the variable name studName is referred to as an identifier. In JavaScript, the var keyword is used to create a variable by allocating memory to it. A keyword is a reserved word that holds a special meaning in JavaScript.
You can initialize the variable at the time of creating the variable or later. Initialization refers to the task of assigning a value to a variable. Once the variable is initialized, you can change the value of a variable as required.
Variables allow keeping track of data during the execution of the script. While referring to a variable, you are referring to the value of that variable. In JavaScript, you can declare and initialize multiple variables in a single statement.
The following syntax demonstrates how to declare variables in JavaScript:
Syntax:
where,
- var: ls the keyword in JavaScript.
- variableName: This is a valid variable name.
The following syntax demonstrates how to initialize variables in JavaScript:
Syntax:
where,
- =: Is the assignment operator used to assign value.
- Value: This is the data to be stored in the variable.
The following syntax demonstrates how to declare and initialize multiple variables in a single statement, which are separated by commas.
Syntax:
Two variables namely, studID and studName and assign values to them.
This code assigns values to studID and studName variables by using the assignment operator (=). The value named David Fernando is specified within double quotes.
How to declare and initialize multiple variables in a single statement in JavaScript.
2. Variable Naming Rules
You cannot refer to a variable until it is created in JavaScript. JavaScript is a case-sensitive language. This means that if you specify X and x as variables, they are treated as two different variables. Similarly, in JavaScript, there are certain rules, which must be followed while specifying variable names.
These rules for a variable name are as follows:
- It can consist of digits, underscore, and alphabets.
- Must begin with a letter or the underscore character.
- Cannot begin with a number and cannot contain any punctuation marks.
- Cannot contain any kind of special characters such as +, *, %, and so on.
- Cannot contain spaces.
- Cannot be a JavaScript keyword.
It is recommended to give meaningful names to variables such that the name determines the kind of data stored in the variable.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0