New Tutorials:   JAVASCRIPT    SASS/SCSS    PL/SQL  
See the Tutorial List

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.