Variables
Variables are used to store values and data types. They work like containers to store something. Javascript is a dynamically typed language, that’s why Variables can have any data type. you don’t need to declare the data type before initialising the variable
There are three ways to declare variables in javascript according to their scope :
var: var keyword was used in older codes to declare variables but after ES6 (ECMAScript 6) it is not recommended to use this keyword. variables declared with var keywords are globally scoped and can be accessed from any line of code, this can create bugs inside of your code that’s why people don’t use it often .var can be updated and redeclared within its scope. You can initialise var at any time without declaring.
let: let keyword is widely used to declare variables since it creates block-scoped variables. Block-scoped variables cannot be accessed from any line of code. You can initialise it at any time. Let can only be updated within its scope and you cannot redeclare the let variable.
const: const keyword is used to declare values whose value cannot be changed at any time. If you have a value which is you don’t want to change throughout your code you should use the const keyword to declare a variable. You cannot redeclare nor update the const variable. const must be initialised while declaring the variable.
To declare a variable in javascript you have to follow some rules :
Variable names are case-sensitive. Variable_Name ≠ variable_name.
You can use letters, digits, _(underscores) and $(dollar sign) to declare variables.
Variable names cannot start with digits. e.g. 6_array is not a valid variable name.
Javascript reserved words cannot be used to declare variables e.g. let let =10 will give you an error.
Syntax to declare a variable :
Let variable_name="Hello world"
const pieValue = 3.14
var athelete = "Mionel Lessi"