Example: Declaration and initialization
Here is a small example of variable declarationTo give the name of a variable or constant and possibly the type of data. and initializationTo give the initial value of a variable or constant.. In this type of JavaScript panel, the top pane contains JavaScript and the bottom pane is the consoleA window used for debugging, usually not visible on the page.. Click on the Run button to run the program.
The variables small
and large
have been both declared and initialized. In contrast, the variable medium
has been declared but not initialized.
When the values are written to the console, you can see that redeclaring small
in line 3 has not caused a problem, and that the value of the uninitialized variable medium
is undefined
.
Notice that the different data typesA type of data, such as a number or a string. are colour-coded with different colours in the console.
Now remove the semicolon at the end of the last line. When you select that line, you will see a little letter i appear to the left of the line number. After you have clicked in the pane, you can hover your cursor over the letter to see a message warning you about the missing semicolon. (Remember, not all missing semicolons are serious enough to keep the program from working.) In the future you might also see triangles and squares that indicate more serious warnings. More information can be found on the Help page about JavaScript panels.
Note:
Hints:
- undefined