المعرفة:: JavaScript الحالة::مؤرشفة المراجع:: JavaScript Essential Training
Variables and Data Types
- Naming Objects: The creation of containers to put objects within.
- Variable: A container with some piece of data.
var
The var
statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
let
The let statement declares a block-scoped local variable, optionally initializing it to a value.
Which to use?
- For most situations, when you want to use a changeable or mutable variable, you should use a let.
- That includes when you want to use it in global scope, because when you declare a let in global scope, it will apply everywhere except where you re-declare it.
- The var is only really useful if you want a mutable variable with global scope all the time, but that situation is quite rare and is a special case.
- So the default is, when you want a changeable or mutable variable use a let.
const
Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can’t be changed through reassignment, and it can’t be re-declared. However, if a constant is an object or array, its properties or items can be updated or removed.