JavaScript Code Structure

Code Structure of JavaScript

In this Codingcompiler JavaScript tutorial, we will look at the general structure of the code, the commands and their separation. Let’s start learning JavaScript.

Code Structure in JavaScript

Previously, we have already seen an example of the command: alert('Hello World!')displays a message.

In order to add another command to the code – you can put it after the semicolon.

For example, instead of one call for alert we will call two alerts:

alert('Hello'); alert('World');

As a rule, each command is written on a separate line – so the code is better to read:


Semicolon in JavaScript

In many cases, a semicolon can be omitted if there is a transition to a new line.

So it will also work:


In this case, JavaScript interprets the transition to a new line as a command separator and automatically inserts a “virtual” semicolon between them.

However, it is important that “in many cases” does not mean “always”!

For example, run this code:

alert(3 +
+ 2);

Displays 6.

That is, the semicolon is not put. Why? Intuitively, it is a matter of “incomplete expression”, the end of which is expected by JavaScript from the first line and therefore does not put semicolons. And here it is, perhaps, good and pleasant.

But in some important situations, JavaScript “forgets” to insert a semicolon where it is needed.

There are not many such situations, but errors that appear in this case are quite difficult to detect and correct.

Not to be unfounded, here is a small example.

This code works:

[1, 2].forEach(alert)

He brings in turn 12. Why it works is not important now, we will understand later.

It is important that such code will not work:

alert("There will be a mistake now")
[1, 2].forEach(alert)

Only the first one will be displayed alert, and then an error. Because there is no semicolon in front of the square bracket JavaScript, but just here it is needed (oops!).

If you put it, then everything will be fine:

alert( "There will be a mistake now" );
[1, 2].forEach(alert)

Therefore, in JavaScript it is recommended to put semicolons. Now this is, in fact, the standard followed by all big projects.

Comments in JavaScript

Over time, the program becomes large and complex. There is a need to add comments that explain what is happening and why.

Comments can be anywhere in the program and do not affect its performance. The JavaScript interpreter simply ignores them.

Single-line comments begin with a double slash //. The text is considered a comment to the end of the line:

// The command below says "Hello"
alert( 'Hello' );

alert( 'World' ); // The second message is displayed separately

Multi-line comments begin with an asterisk «/*»and end with an asterisk-slash «*/», like this:

/* An example with two messages.
This is a multi-line comment.
alert( 'Hello' );
alert( 'World' );

All comment content is ignored. If you put the code inside /* … */or after //– it will not be executed.

/* Commented out the code
alert( 'Hello' );
alert( 'World' );

Use hotkeys!

In most editors, you can put a comment with a hot key, usually for single-line ones and something like for multi-line comments (you need to select a block and press a key combination). See the editor’s guide for details.Ctrl+/Ctrl+Shift+/Nested comments are not supported!

There will be an error in this code:

  /* nested comment ?!? */

Do not be afraid to comment. The more code in the project – the more important they are. As for the increase in the size of the code – it is not scary, because There are JavaScript compression tools that will easily remove them when publishing code.

In the coming JavaScript tutorials we will talk about variables, blocks, and other structural elements of a JavaScript program.

Related JavaScript Tutorials For Beginners

JavaScript Introduction Tutorials
Introduction to JavaScript
Javascript Reference and Specifications
Javascript Code editors
Javascript Developer Console
JavaScript Basics Tutorial
JavaScript Hello World.!
External JavaScript Files

Leave a Comment