- Variables declared with
varare function-scoped, meaning they are only accessible within the function where they are declared, or globally if declared outside of any function.
var myVar = 10;
let(introduced in ECMAScript 2015, ES6):
letallows you to declare block-scoped variables, which means they are limited to the block or statement in which they are declared.
let myVar = 10;
const(introduced in ECMAScript 2015, ES6):
constis used to declare variables that should not be reassigned once they are assigned a value. It is also block-scoped.
const pi = 3.14159;
- Variable names (identifiers) are case-sensitive.
- Variable names can consist of letters, numbers, underscores, and dollar signs but must start with a letter, underscore, or dollar sign.
function) that cannot be used as variable names.
- Variables can hold different data types, such as numbers, strings, arrays, objects, functions, and more.
- You can reassign a value to a variable declared with
let, but you cannot reassign a value to a variable declared with
- It’s good practice to use
constby default and only use
letwhen you need to reassign a variable’s value.
This code declares a variable
message, assigns a string value to it, and then logs the value to the console. Later, it reassigns a new value to the