JavaScript Variables
4 Ways to Declare a JavaScript Variable:
- Using
var
- Using
let
- Using
const
- Using nothing
What are Variables?
Variables are containers for storing data (storing data values).
In this example, x
, y
, and z
, are variables, declared with the var
keyword:
var x = 5;
var y = 6;
var z = x + y;
alert("value of z is : " + z);
</script>
In this example, x
, y
, and z
, are variables, declared with the let
keyword:
<script>
let x = 5;
let y = 6;
let z = x + y;
alert("value of z is : " + z);
</script>
In this example, x
, y
, and z
, are undeclared variables:
<script>
x = 5;
y = 6;
z = x + y;
alert("value of z is : " + z);
</script>
From all the examples above, you can guess:
- x stores the value 5
- y stores the value 6
- z stores the value 11
When to Use JavaScript var?
Always declare JavaScript variables with var
,let
, orconst
.
The var
the keyword is used in all JavaScript code from 1995 to 2015.
The let
and const
keywords were added to JavaScript in 2015.
If you want your code to run in an older browser, you must use var
When to Use JavaScript const?
If you want a general rule: always declare variables with const
.
If you think the value of the variable can change, use let
.
In this example, price1
, price2
, and total
, are variables:
<script>
const price1 = 5;
const price2 = 6;
let total = price1 + price2;
alert("value of total is : " + total);
</script>
The two variables price1
and price2
are declared with the const
keyword.
These are constant values and cannot be changed.
The variable total
is declared with the let
keyword.
When we declare the variable with var and let we can be changed.
JavaScript Identifiers
All JavaScript variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, total volume).
The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter
- Names can also begin with $ and _ (but we will not use it in this tutorial)
- Names are case sensitive (y and Y are different variables)
- Reserved words (like JavaScript keywords) cannot be used as names
JavaScript Data Types
JavaScript variables can hold numbers like 100 and text values like "John Doe".
In programming, text values are called text strings.
JavaScript can handle many types of data, but for now, just think of numbers and strings.
Strings are written inside double or single quotes. Numbers are written without quotes.
If you put a number in quotes, it will be treated as a text string.
const pi = 3.14;
let person = "John Doe";
let answer = 'Yes I am!';
Declaring a JavaScript Variable
Creating a variable in JavaScript is called "declaring" a variable.
You declare a JavaScript variable with the var
or the let
keyword:
var carName;
or
let carName;
After the declaration, the variable has no value (technically it is undefined
).
To assign a value to the variable, use the equal sign:
carName = "Volvo";
One Statement, Many Variables
You can declare many variables in one statement.
Start the statement with let
and separate the variables by a comma:
let person = "John Doe", carName = "Volvo", price = 200;
A declaration can span multiple lines:
let person = "John Doe",
carName = "Volvo",
price = 200;
A variable declared without a value will have the value undefined
.
Variable Scope :
When we declare the variable with var or without var, it default has global scope.
let has local scope when we declare the variable with let its has always local scope.
Example:
when we declare the variable with Var and let
var name = "Script revision"
so when you want to access this variable that variable you will be able to see it in the windows section. windows are a global scope that is created by a javascript engine.
console.log(window.name) // it prints the script revision
but when you create the var with let :
let name ="script revision"
Console.log(window.name) // wont print anythings.
0 Comments