If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In JavaScript, variables are used to store data values. They are like containers that hold values, which can be used later in the program. Variables can be declared using the var
, let
, or const
keyword.
The var
keyword was used to declare variables in JavaScript before the release of ES6. It has a function-level scope, which means that it can be accessed within the function it is declared in. If a variable is declared outside of a function using the var
keyword, it will be accessible globally.
The let
keyword was introduced in ES6 and has a block-level scope, which means that it can be accessed within the block it is declared in. A block is a set of statements enclosed in curly braces {}
. Variables declared with let
can be updated but not redeclared.
The const
keyword is used to declare variables whose values are constant and cannot be changed. Like let
, const
has a block-level scope. Once a value is assigned to a variable using the const
keyword, it cannot be reassigned.
Here's an example of declaring a variable using var
, let
, and const
:
javascriptCopy code
// using var
var x = 5;
function myFunction() {
var y = 10;
console.log(x + y); // output: 15
}
console.log(x); // output: 5
console.log(y); // output: Uncaught ReferenceError: y is not defined
// using let
let a = 15;
if (true) {
let b = 10;
console.log(a + b); // output: 25
}
console.log(a); // output: 15
console.log(b); // output: Uncaught ReferenceError: b is not defined
// using const
const PI = 3.14;
console.log(PI); // output: 3.14
PI = 3.14159; // output: Uncaught TypeError: Assignment to constant variable.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.