JS Variables

JS Variables

JS Variables

In JavaScript, variables are used to store values that can be used and manipulated in your code. Variables are declared using the let, const, or var keywords, followed by the variable name, and optionally an initial value.

Here are some key aspects of JavaScript variables:

  • Declaring variables: JavaScript variables can be declared using the let, const, or var keywords. For example:
				
					let x; // declare a variablenamed x

const PI = 3.14; // declare aconstant named PI with an initialvalue of 3.14

var y = "Hello"; //declare a variable named y with aninitial value of "Hello" 
				
			
  • Assigning values to variables: Once a variable is declared, you can assign a value to it using the assignment operator (=). For example:
				
					

x = 5; // assign the value 5 tothe variable x

y = "World"; // assignthe value "World" to the variable y 
				
			
  • Data types: JavaScript variables can hold values of different data types, including numbers, strings, booleans, arrays, objects, and more.
  • Naming conventions: JavaScript variable names can contain letters, digits, underscores, and dollar signs. They cannot begin with a digit. It is recommended to use descriptive names for your variables that indicate their purpose, and to use camelCase or snake_case for multi-word variable names.
  • Scope: JavaScript variables have different scopes, which determines where in your code they can be accessed. Variables declared with let and const have block scope, while variables declared with var have function scope.

Here’s an example of a JavaScript program that declares and uses a variable:

				
					let greeting = "Hello,world!";

console.log(greeting);
				
			

Let:

let is a keyword in JavaScript that is used to declare a block-scoped variable. Block scope means that the variable is only accessible within the block of code in which it is declared. A block of code is defined by a pair of curly braces {}.

Here’s an example of declaring and using a let variable:

				
					function myFunction() {

let x = 5; // Declare a variable x with the value 5

if (true) {

    let y = 10; // Declare a variable y with
the value 10 within the block of code defined by the if statement

    console.log(x + y); // Output:15

}

console.log(x); // Output: 5

console.log(y); // Uncaught ReferenceError: y is not defined (because y
is only accessible within the block of code defined by the if statement)
} 
				
			

In this example, we declare a function myFunction that contains two let variables: x and y. The variable x is declared within the function block and can be accessed anywhere within the function, while the variable y is declared within the block of code defined by the if statement and can only be accessed within that block.

let variables can also be re-assigned a new value after they have been declared, like this:

				
					let x = 5; // Declare a variable x with the value 5

x = 10; // Re-assign the value of x to 10

console.log(x); // Output: 10
				
			

Overall, let is a useful keyword in JavaScript that allows you to declare block-scoped variables that can be re-assigned new values.

Const:

const is a keyword in JavaScript that is used to declare a constant variable. A constant variable is similar to a let variable, but its value cannot be re-assigned after it has been declared. The variable remains read-only and its value cannot be changed throughout the lifetime of the program.

Here’s an example of declaring and using a const variable:

				
					const PI = 3.14; // Declare a
constant variable named PI with the value 3.14

console.log(PI); // Output: 3.14

PI= 3.14159; // Uncaught TypeError: Assignment to constant variable. (because youcannot re-assign the value of a constant variable)
				
			

In this example, we declare a const variable named PI and assign it the value 3.14. We then attempt to re-assign the value of PI to 3.14159, but this results in a TypeError because PI is a constant variable and cannot be re-assigned.

It’s important to note that while the value of a constant variable cannot be re-assigned, it does not mean that the value itself is immutable. If a constant variable holds an object or an array, for example, the properties or elements of the object or array can still be modified.

Here’s an example of declaring a constant variable that holds an array:

				
					const myArray = [1, 2, 3]; //
Declare a constant variable named myArray that holds an array

myArray.push(4); // Add a new element to the array

console.log(myArray); // Output:[1, 2, 3, 4]
				
			

In this example, we declare a const variable named myArray that holds an array. We then add a new element to the array using the push() method, and output the updated array to the console. While the value of the myArray variable cannot be re-assigned, the elements of the array can still be modified.

Overall, const is a useful keyword in JavaScript that allows you to declare read-only constants that cannot be re-assigned after they have been declared. This can help make your code more secure and prevent accidental changes to critical values.

Join To Get Our Newsletter
Spread the love