Last Updated on March 11, 2024 by
JavaScript Variables
- In JavaScript, a variable is a container used to store data values.
- Variables are declared using keywords such as “var”, “let”, or “const”, followed by the variable name.
- The value of the variable can be assigned using the assignment operator “=”.
- For example, the following code declares a variable `x` and assigns a value `5` to it: var x = 5;
- Variables in JavaScript can hold different types of data, such as numbers, text strings, booleans, arrays, and objects. The type of the variable can change dynamically during the runtime of the program.
- JavaScript variables are used to store and manipulate information in a program.
JavaScript Keywords :-
– Var Keyword
- Variables declared using the var keyword are either globally or functionally scoped, and do not support block-level scope, if a variable is defined in a loop or an if statement, it can be accessed outside the block and accidentally redefined leading to a buggy program. On the other hand, let and const keywords are block-scoped, meaning a variable declared using let or const is only accessible within the block it was declared.
- Variable names are case-sensitive in JavaScript, meaning variable names like msg, MSG, Msg, and mSg are considered separate variables. Variable names can contain letters, digits or the symbols $ and _. A variable name cannot start with a digit 0-9. A variable name cannot be a reserved keyword in JavaScript, e.g. var, function, and return cannot be variable names [Source 2].
- To declare a variable, one can use the var keyword followed by the variable name and the value assigned to it, as shown below: var age = 25;
- The var keyword was JavaScript’s first way to declare a variable, but it has some baggage.
- JavaScript variables were always defined at the function scope, which meant that variables were global. JavaScript didn’t enforce block-level scopes such as inside a for loop or if block. So a variable declared with var would be hoisted to the top of its function scope.
– Let Keyword
- The `let` keyword in JavaScript is used to declare block-scoped variables. This means that variables declared with `let` are only accessible within the block they are declared in (including any nested blocks), and are not accessible outside of that block.
- Let us see the syntax for this, let name1 [= val1] [, name2 [= val2]] [, …, namen [= valn];
- JavaScript only had function-scoped variables declared using the `var` keyword, which meant that variables declared with `var` were accessible throughout the entire function, regardless of where they were declared. With `let`, developers can now declare variables with a more fine-grained scope, making their code easier to reason about.
Here is an example of using `let` to declare a variable within a block:
function foo() {
if (true) {
let x = 10;
console.log(x); // output: 10
}
console.log(x); // output: Uncaught ReferenceError: x is not defined
}
In this example, the variable `x` is only accessible within the `if` block and cannot be accessed outside of it.
– Const Keyword
- The `const` keyword in JavaScript is used to declare a variable that cannot be reassigned after its initial assignment. Once a variable is declared with the `const` keyword, its value cannot be changed.
- It is a block-scoped variable, meaning that its scope is limited to the block in which it is declared.
Here are some key features of the `const` keyword in JavaScript:
- A `const` variable must be assigned a value when it is declared. If it is not assigned a value, it will result in a `SyntaxError`.
- Once a `const` variable is assigned a value, its value cannot be changed. Attempting to reassign a `const` variable will result in a `TypeError`.
- A `const` variable is block-scoped, meaning that its scope is limited to the block in which it is declared.
- A `const` variable can be used to declare an array or object, but its properties or elements can still be modified.
- The `const` keyword is used to declare a variable that is intended to have a constant value throughout the program.
Here is an example of declaring a `const` variable:
const PI = 3.14;
- In this example, `PI` is assigned the value of `3.14`, and cannot be reassigned to a different value later in the program.
Here is an example of declaring a `const` variable with an object:
const person = {
name: “John”,
age: 30,
};
- In this example, `person` is assigned an object with two properties: `name` and `age`. Although the `person` variable cannot be reassigned to a different object, the properties of the `person` object can still be modified.
Note :-
- If you want to learn Javascript, Above are the key points which will help you to grow more in your career.
- Also, if you like this post or have any queries, Do let me know in the comments.
- Below are the resources to learn more about Javascript.