We used the following 2 commands:
- alert ('Hello World');
- document.write ('Hello World');
With the first command we get an output of the following content on the screen in a separate pop-up window and with the second output directly in the content area of the browser window.
Order of processing our commands
The program is processed from top to bottom. That's why we see “HalloWelt” as an output. If a program were to run from bottom to top, “WorldHello” would have to be output, which would not be particularly logical.
Although it is logical, it still has to be mentioned once.
The inconspicuous semicolon at the end of each command is extremely important. This separates the command from the following.
Therefore we could write all commands on one line and we get the same result in the output.
But only if we don't accidentally forget the delimiting semicolon. In the following example we don't get any output and so we are surprised:
We do not receive an error message directly or any output. The error message "Uncaught Syntax Error: unexpected identifier" is hidden deep in the executing browser. How we get this hidden error message visible, comes in a following chapter.
Now let's move on to the basic structure.
ALWAYS include a semicolon at the end to avoid unpleasant surprises.
It is often clever to look at commands from right to left. In our example there is an output:
Next to our semicolon is the output text in round brackets and in quotation marks.
The quotation marks are important because this is text that we want to output. We don't yet know any variables - in the Variables chapter we see that otherwise the text would be understood as a variable.
The type of quotation marks
The type of quotation marks within the brackets is irrelevant for pure text output - the main thing is that there are quotation marks. So we can use double quotation marks (") as well as single quotation marks ('). It is only important that the same kind of quotation marks are always used. A combination of double and single quotation marks such as goes wrong!
So if something goes wrong here, look carefully whether and whether the correct quotation marks are used.
In our examples an output should be made, regardless of whether it is above or above.
This is the common, space-saving variant that all programmers normally use.
If you want to confuse old hands among programmers, insert spaces.
Funnily enough, it works in the browser - but it's not really clear. So just stick to the conventions.
Missing space in the output
Anyone who has wondered why the space between our words "Hello" and "World" was missing in the output - here is the explanation:
The browser outputs exactly what we ask of it on the screen.
If we want a space between "Hello World", then we have to include this in our code:
- Why don't people prefer e-books for novels?
- How to smoke a beef brisket
- What does Access Denied
- What is pH and pOH
- What is the power dissipation formula
- What is business acumen
- Narcissists have mean spiritual leaders
- Why does the world remember D-Day
- Should India annex Nepal 1
- Did the Ramones write their own songs
- What degrees does West Point offer
- When does suffering become unbearable?
- Why are quasars interesting to see
- How does an acid wash an email
- What is historical fantasy
- What is the Access Governance Suite
- When does a shear force occur
- It was all Anna's fault in Frozen
- What was Theia's role in Greek mythology
- Will Airbnb ever arrange temporary leases of office space?
- What if the war of the worlds happened
- Which song is similar to Dhadak
- A plant cell changes its shape
- What do wedding colors mean