In JavaScript, variables are containers for storing data values. They are declared using 'var',' let', or 'const' keywords and can hold various types of information such as numbers, strings, or objects.
In JavaScript, variables can be implicitly declared without using keywords like 'const', 'let', or 'var', but doing so may lead to unintended consequences and is generally discouraged for clarity and best coding practices.
Syntax: keyword variableName = value
const jersey=10;
let name="messi";
var team="MLS@10";
team="Argentina"
const & let
Variables declared with 'let' and 'const' are block-scoped
Block scope refers to the visibility of variables within curly braces {}.
They are only accessible within the block where they are defined.
function variables(){ let name="sam"; const age=24; } console.log(name,age) //ReferenceError: name is not defined
Constant*: const*
It retains a fixed value throughout its scope and cannot be reassigned,
It provides a stable reference to unchanging data.
const name="sajid" name="hussain" console.log(name); //TypeError: Assignment to constant variable.
let
After initializing a variable with 'let' its value can be altered by assigning a different value.
It allows flexibility in data manipulation.
let name="sajid" name="hussain" console.log(name); //output - hussain
Undefined
undefined variable is one that has been declared but not assigned a value.
Accessing or using such a variable will result in an 'undefined' value.
let car; console.log(car); //undefined