Data
Let’s start off by looking at the building blocks of any language: the types. JavaScript programs manipulate values, and those values all belong to a type. JavaScript’s types are:
Variable
New variables in JavaScript are declared using one of three keywords: let
, const
, or var
.
Let
let
allows you to declare block-level variables. The declared variable is available from the block it is enclosed in.
The following is an example of scope with a variable declared with let
:
Const
const
allows you to declare variables whose values are never intended to change. The variable is available from the block it is declared in.
Var
var
does not have the restrictions that the other two keywords have. This is because it was traditionally the only way to declare a variable in JavaScript. A variable declared with the var
keyword is available from the function it is declared in.
An example of scope with a variable declared with var
:
If you declare a variable without assigning any value to it, its type is undefined
.
An important difference between JavaScript and other languages like Java is that in JavaScript, blocks do not have scope; only functions have a scope. So if a variable is defined using var
in a compound statement (for example inside an if
control structure), it will be visible to the entire function. However, starting with ECMAScript 2015, let
and const
declarations allow you to create block-scoped variables.
Difference Between let
and var
javascript - What is the difference between “let” and “var”? - Stack Overflow
let
is visible to the scopevar
is visible to the function, and it will create a property on the global object
Hacks
- Use
+
prefix to convert something to number