Master Code Structure Quickly and Easily in JavaScript

Master Code Structure Quickly and Easily in JavaScript

Hey, Amazing ones

youre-welcome.gif (498×333)

Hello everyone, my name is Aryan Sharma and I hope you're all doing well. I'm excited to kick off this JavaScript course for absolute beginners with my second blog post.

In the first blog, we learned about script tags.

You can find the First blog here↗️

So, Let's get started!!

lets-start-text.gif (360×258)

Here we go..

📌Statements

A statement is a single instruction in a JavaScript program. Statements are executed one at a time, in the order they are written in the code.

We can have as many statements in our code as we want. Statements can be separated with a semicolon.

For example, here we split “Hello Aryan” into two alerts:

alert('Hello'); alert('Aryan');

Usually, statements are written on separate lines to make the code more readable:

alert('Hello');
alert('Aryan');

Code will execute one by one and be separated by a compiler.

📌Semicolons

Semicolons are used to separate statements/instructions.

They are not always required, as the JavaScript interpreter will automatically insert them in some instances, but including them is generally considered good practice.

Let's understand with the help of a code

var x = 0;
x++;

Don't get confused with this var, we will cover this in detail later on!

The first statement declares a variable named x and assigns it the value 0. The second statement increments the value x by 1.

Semicolons are optional in JavaScript, but it is generally a good practice to use them. This makes your code more readable and helps to avoid errors.

This would also work:

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

Here, JavaScript interprets the line break as an “implicit” semicolon. This is called an automatic semicolon insertion.

⛔In most cases, a newline implies a semicolon. But “in most cases” does not mean “always”!

There are cases when a newline does not mean a semicolon. For example:

alert(1 +
2
+ 3);

The code outputs 6 because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus "+", then it is an “incomplete expression”, so a semicolon there would be incorrect. And in this case, that works as intended.

But there are some situations where JavaScript fails to assume semicolon ; when it is highly needed in the code.

var x = 5
[1, 2, 3].forEach(function(num) {
  console.log(num + x)
})

In this case, a semicolon should be added after var x = 5 to prevent automatic semicolon insertion from causing a syntax error.

We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let’s note once again – it is possible to leave out semicolons most of the time. But it’s safer – especially for a beginner – to use them.

📌Comments

As we proceed with our code, our script code keeps getting complex and in order to reduce complexity, we add comments in the code to add necessary information about what the code is doing and why.
This is a good practice to make code understandable for other programmers.✅

Comments can be put into any place of a script. They don’t affect its execution because the engine simply ignores them.

Basically, we have two types of comments

One-line comments start with two forward slash characters //.

Let's see

// This comment occupies a line
alert('Aryan');

alert('Sharma'); // This comment follows the statement

Multiline comments start with a forward slash and an asterisk /* and end with an asterisk and a forward slash */.

Like this:

/* An example with two instructions/message
This is a multiline comment.
*/
alert('Hello');
alert('World');

The content of comments is ignored, so if we put code inside /* … */, it won’t execute.

Sometimes it can be handy to temporarily disable a part of code:

Apply comments using your Keyboard-

Press Ctrl + / to make it handy.

Later in the course, there we will explain how to write better comments.

Wonders🎉 here we have covered the Statements, Semicolons & Comments of our JS course.

I Hope you loved the blog❣️

See u in the next blog...Stay tuned🎶

Don't forget to follow me on:
Twitter LinkedIn