Skip to main content

Command Palette

Search for a command to run...

JavaScript : Variables.

Published
•1 min read•View as Markdown
J

I am talented and motivated individual who has completed a Master of Computer Applications (MCA) degree and is now pursuing a career in full-stack web development. With a passion for technology and a desire to innovate, I am constantly seeking new challenges and opportunities to enhance my skills and stay at the forefront in Tech field.

Variables are used to store values in JavaScript. They can be created using the var, let, or const keywords.

let, const and var

The var keyword is used to declare a variable that is scoped to the nearest function or block. The let keyword is similar to var, but it is block-scoped rather than function-scoped. The const keyword is used to declare a variable that cannot be reassigned.

Here's an example of how to declare and use variables in JavaScript:

let name = 'John';
console.log(name);  // Output: 'John'

name = 'Jane';
console.log(name);  // Output: 'Jane'

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

PI = 3.14159;  // This will throw an error

It's important to choose the appropriate keyword for declaring variables based on how you plan to use them. For example, you might use const for variables that you don't want to change, and let for variables that you need to reassign.