JavaScript Syntax

JavaScript has its own syntax and programming style. Syntax of a language defines rules of writing the code in that language, what is the correct way and what is not. In this tutorial, we will learn about the basic syntax for writing code in JavaScript.

JavaScript uses its own syntax to create variable, function, statements, etc. but if you have knowledge of any programming language like C, C++ or Java, it won’t be very difficult for you to understand the syntax of JavaScript.

1. A semicolon at the end of every statement (is Optional)

Semicolons are used to terminate the JavaScript code statements. A code statement is a line of code and we can mark the end of each code line by adding a semicolon. In languages like C, C++, Java, etc. we use a semicolon at the end of each code statement.

In JavaScript, statements are generally followed by a semicolon(;) which indicates termination of the statement.

However, it is not necessary to use a semicolon(;) at the end of the statement.

The following example shows two valid declarations of a JavaScript statement:

var var_name = value;

var var_name = value

In this example, the first statement is terminated using a semicolon, while the second statement is terminated without any semicolon. Both statements are valid in Javascript.

2. JavaScript White Spaces and Line Breaks

JavaScript interpreter ignores the tabs, spaces, and newlines that appear in the script, except for strings and regular expressions.

Below we have a simple code example where we have added an extra line break after each code statement and then the code without any space at all. Both are acceptable in JavaScript.

var i = 10;

var j = 20;

var a = i+j; var b = i-j;

You can write a more clean and readable JavaScript code by using multiple spaces as JavaScript ignores multiple spaces.

3. JavaScript Case Sensitivity

JavaScript is a case sensitive language, which means all the keywords, function names, variable names or identifiers should be typed with the consistent casing of letters. In general, a literal should start with a small letter and to combine the long name, we can use the camel case style too.

Let’s take an example, in the code below, all the three variables are distinct from each other because the first one is in the lower case, the second one starts with a capital letter, and the third one is in the upper case.

var marks;

var Marks;

var MARKS;

In camel-case style, all alphabets are in small expect for the starting alphabet of words, and the first alphabet will always be small. So if I want to name a variable or a function as myfirstvariable, the camel case style would be myFirstVariable.

4. JavaScript Comments

Comments refer to the text or code in a program that is ignored at the time of executing the program. Comments are used to provide additional information in the code, such as the description of code. And it is considered as good practice to add comments to your code.

Similar to other programming languages like C, C++, etc., JavaScript also defines two types of comments.

  • Single line Comments
  • Multiline Comments

Let’s take an example and understand how we can add these in our JavaScript code.

<html>
<head>
    <title>JavaScript Comment Example</title>
    <script>
    // This is an single line comment and it will not be executed
    /*
        This is a multiline comment and everything written 
        inside this block will not be executed. 
    */
    var x = 10;
    var y = 2*x;
    </script>
</head>
<body>
    <!-- Some HTML code - This is HTML comment -->
</body>
</html>

5. JavaScript Statements

Statements are known as the instructions in any programing language, these statements are set to be executed by the computer as a computer program.

JavaScript code is nothing but a list of these programming statements, these statements are collections of values, operators, expressions, keywords, and comments.

<html>
<head>
    <title>JavaScript Statement</title>
</head>
<body>
        <script>
         document.write("this is a text") ;   // JavaScript statement
         var a = 10+20;    // Another Statement
         document.write(a);
        </script>
</body>
</html>

In this tutorial, we explained the basic Javascript syntax and its usage with the help of examples. This will help you in getting a basic idea about writing the JavaScript code. We will be learning more about different JavaScript features like defining a variable, using operators, defining functions in upcoming tutorials and will cover the syntax for them in the respective tutorials.

C++ comments line

In this tutorial, we will learn about C++ comments, why we use them, and how to use them with the help of examples.

C++ comments are hints that a programmer can add to make their code easier to read and understand. They are completely ignored by C++ compilers.

There are two ways to add comments to code:

// – Single Line Comments

/* */ -Multi-line Comments


Single Line Comments

In C++, any line that starts with // is a comment. For example,

// declaring a variable
int a;

// initializing the variable 'a' with the value 2
a = 2;

Here, we have used two single-line comments:

  • // declaring a variable
  • // initializing the variable 'a' with the value 2

We can also use single line comment like this:

int a;    // declaring a variable

Multi-line comments

In C++, any line between /* and */ is also a comment. For example,

/* declaring a variable
to store salary to employees
*/
int salary = 2000;

This syntax can be used to write both single-line and multi-line comments.


Using Comments for Debugging

Comments can also be used to disable code to prevent it from being executed. For example,

#include <iostream>
using namespace std;
int main() {
   cout << "some code";
   cout << ''error code;
   cout << "some other code";

   return 0;
}

If we get an error while running the program, instead of removing the error-prone code, we can use comments to disable it from being executed; this can be a valuable debugging tool.

#include <iostream>
using namespace std;
int main() {
   cout << "some code";
   // cout << ''error code;
   cout << "some other code";

   return 0;
}

Pro Tip: Remember the shortcut for using comments; it can be really helpful. For most code editors, it’s Ctrl + / for Windows and Cmd + / for Mac.


Why use Comments?

If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code.

Note: Comments shouldn’t be the substitute for a way to explain poorly written code in English. We should always write well-structured and self-explanatory code. And, then use comments.

As a general rule of thumb, use comments to explain Why you did something rather than How you did something, and you are good.