Variable and Constant in JavaScript
Variable
In JavaScript, a variable is a container that is used to store value. Variables are declared using the keywords var and let which indicate the scope and the value of the variable can be changed.
var:
Variables
declared using the var keyword are function scoped. This means that they are
only accessible within the function in which they are declared. If a variable
is declared with the same name in multiple functions, it is considered different variables within each function.
The
syntax for declaring a variable using var is as follows:
Syntax:variable
var
variableName;
For
Example:
var
x; // declares a variable called x
let:
Variables
declared using the let keyword are block scoped. This means that they are only
accessible within the block in which they are declared. If a variable is
declared with the same name in multiple blocks, it is considered a different
variable within each block.
The
syntax for declaring a variable using let is as follows:
Syntax:
let
variableName;
Example:
let
x; // declares a variable called x
You
can also assign a value to a variable when you declare it using the assignment
operator (=).
For
example:
let
x = 5; // declares a variable called x and assigns it the value 5
Constant
The constant is an entity whose value remains always the same. In JavaScript, we can assign the constant value by using a const
const:
Variables
declared using the const keyword are also block-scoped. The only difference
between let and const is that variables declared with const cannot be
reassigned. Once a value is assigned to a variable declared using const, it
cannot be changed.
The
syntax for declaring a constant variable is as follows:
Syntax:
const
variableName = value;
Example:
const
PI = 3.14; // declares a constant variable called PI with the value 3.14
You
cannot reassign a value to a constant variable once it has been declared. If
you try to do so, you will get a TypeError.
For
example:
const
PI = 3.14;
PI
= 3.14159; // TypeError: Assignment to constant variable.
JavaScript
supports a variety of data types, including numbers, strings, and booleans.
For
example:
let
num = 42; // number
let
str = 'Hello'; // string
let
flag = true; // Boolean
JavaScript
also supports special data types like arrays and objects, which are used to
store collections of data.
For
example:
let
arr = [1, 2, 3]; // array
let
obj = { name: 'Bob', age: 30 }; // object
In
JavaScript, best practice is to use let when the variable's value will change
and const when it will not change. This provides more clarity to the code and
helps to prevent accidental reassignments.
Rules of variable naming:
In
JavaScript, there are a few rules that you should follow when naming variables:
i)
Variable names can only contain letters, numbers, and the $ and _ characters.
They cannot contain spaces or other special characters.
ii)
Variable names must start with a letter, $, or _. They cannot start with a
number.
iii)
Variable names are case-sensitive. This means that x and X are two different
variables.
iv)
Variable names cannot be reserved words. Reserved words are words that are
reserved by JavaScript and cannot be used as variable names. Examples of
reserved words include function, if, else, and while.
v)
Variable names should be descriptive and meaningful. They should reflect the
purpose of the variable and make your code easier to understand.
Here
are some examples of valid variable names in JavaScript:
let
x;
let
y;
let
price;
let
name;
let
_private;
let
$special;
And
here are some examples of invalid variable names:
let
123; // cannot start with a number
let
first-name; // cannot contain hyphen
let
function; // cannot be a reserved word
No comments:
Post a Comment