But, the variable
früh is not the same as
The syntax of comments is the same as in C++ and in many other languages:
Comments behave like whitespace and are discarded during script execution.
var — Declares a variable, optionally initializing it to a value.
let— Declares a block-scoped, local variable, optionally initializing it to a value.
const — Declares a block-scoped, read-only named constant.
You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.
You can use most of ISO 8859–1 or Unicode letters such as å and ü in identifiers (for more details see this blog post). You can also use the Unicode escape sequences as characters in identifiers.
Some examples of legal names are
You can declare a variable in two ways:
- With the keyword
var. For example,
var x = 42. This syntax can be used to declare both local and global variables, depending on the execution context.
- With the keyword
let. For example,
let y = 13. This syntax can be used to declare a block-scope local variable. See Variable scope below.
You can also simply assign a value to a variable For example,
A variable declared using the
let statement with no assigned value specified has the value of
An attempt to access an undeclared variable results in a
ReferenceError exception being thrown:
You can use
undefined to determine whether a variable has a value. In the following code, the variable
input is not assigned a value, and the
if statement evaluates to
undefined value behaves as
false when used in a boolean context. For example, the following code executes the function
myFunction because the
myArray element is undefined:
undefined value converts to
NaN when used in numeric context.
When you evaluate a
null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts. For example:
When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.
5, because the scope of
x is the global context (or the function if the following codes are part of the function), not the immediate
if statement block.
This behavior changes, when using the
let declaration introduced in ECMAScript 2015.
undefined. So even if you declare and initialize after you use or refer to this variable, it still returns
The above examples will be interpreted the same as:
Because of hoisting, all
var statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.
In ECMAScript 2015,
const are hoisted but not initialized. Referencing the variable in the block before the variable declaration results in a
ReferenceError because the variable is in a "temporal dead zone" from the start of the block until the declaration is processed.
For functions, only the function declaration gets hoisted to the top and not the function expression.
Global variables are in fact properties of the global object. In web pages, the global object is
window, so you can set and access global variables using the
Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called
phoneNumber is declared in a document, you can refer to this variable from an iframe as
You can create a read-only, named constant with the
const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign ($) and can contain alphabetic, numeric, or underscore characters.
A constant cannot change value through assignment or be re-declared while the script is running. It must be initialized to a value.
The scope rules for constants are the same as those for
let block-scope variables. If the
const keyword is omitted, the identifier is assumed to represent a variable.
You cannot declare a constant with the same name as a function or variable in the same scope. For example:
However, the properties of objects assigned to constants are not protected, so the following statement is executed without problems.
Also, the contents of an array are not protected, so the following statement is executed without problems
Data structures and types
The latest ECMAScript standard defines eight data types:
- Seven data types that are primitives:
nullis not the same as
NULL, or any other variant.
- undefined. A top-level property whose value is not defined.
- Number. An integer or floating point number. For example:
- BigInt. An integer with arbitrary precision. For example:
- String. A sequence of characters that represent a text value. For example: “Howdy”
- Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.
- and Object
Although these data types are relatively few, they enable you to perform useful functions with your applications.
functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
Data type conversion
And later, you could assign the same variable a string value, for example:
Converting strings to numbers
In the case that a value representing a number is in memory as a string, there are methods for conversion.
parseInt only returns whole numbers, so its use is diminished for decimals. Additionally, a best practice for
parseInt is to always include the radix parameter. The radix parameter is used to specify which numerical system is to be used.
An alternative method of retrieving a number from a string is with the
+ (unary plus) operator: