Signup/Sign In

JavaScript Syntax

JavaScript has its own syntax and programming style. Syntax of a language defines the 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 variables, functions, statements, etc. but if you know any programming language like C, C++, or Java, it won't be very difficult for you to understand the syntax of JavaScript.

So let's see some of the important syntax rules that you should know to write code in JavaScript.

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

Semicolons are used to mark the end of 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 JavaScript statements:

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 in the code, except when they are used in 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 or tabs 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 proper casing of letters. In general, a literal (variable name or function name, etc.) should start with a small letter, and for multiple-word names, you should use the camel case style.

Let's take an example, in the code below, all 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 except 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 program execution. Comments are used to provide additional information in the code, such as the description of the code. It is considered good practice to add comments to your code.

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

  • Single line Comments

  • Multiline Comments

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

    <title>JavaScript Comment Example</title>
    // 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;
    <!-- Some HTML code - This is HTML comment -->

5. JavaScript Statements

Statements are the instructions in any programming language or a single line of code, like declaring a variable or calling a function.

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

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

6. Variables

In JavaScript, variables are containers in which different values can be stored.

To create a variable in JavaScript, you can use the let, var, or const keyword, along with a name for the variable, and create a variable in JavaScript.

let name = "Abhishek";
const PI = 3.14;

In the code example above, name and PI are variables.

7. Data Types

JavaScript supports various types of data like numbers, strings, boolean, undefined, null, and symbols. When you create a variable you can assign it any type of value.

There are some rules for using these values in the code, like a string value is always enclosed within double quotes or single quotes, etc. Check this to learn all about Datatypes in JavaScript.

let marks = 85;    // number value
let msg = "Welcome to Studytonight";    // string value
let isRegistered = true;    // boolean value
let empty = null;    // null value
let notDefined;    // undefined value

8. Scope using { }

In JavaScript, you can define a scope using the curly brackets {}.

You can create a simple block of code using {}, and write code inside it.

    let marks = 85;

The curly brackets are also used with conditional statements, loops, and functions to define their body.

function myMarks()
    let marks = 85;

9. So much more...

Apart from all this, there is so much more in JavaScript. For example,

  1. JavaScript has many operators - JavaScript Operators

  2. You can use conditional statements in JavaScript

  3. You can use loops in JavaScript

  4. Create and use functions

  5. Objects

  6. Class and the concept of OOP is also supported

So follow this tutorial set, you can see the index in the left sidebar or just click on the button below to move on to the next tutorial.

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 JavaScript code. We will be learning more about different JavaScript features like defining a variable, using operators, and defining functions in upcoming tutorials and will cover the syntax for them in the respective tutorials.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight