Javascript Syntax: Rules To Write Code


In this tutorial, you will see the syntax of writing comments, creating variables, assigning value, applying operators, conditionals, loops, functions, keywords, and more.

What is the syntax?

The syntax of any computer language defines how code is written in that language. It is a simple set of rules that is to be followed while writing the code. The combination of these rules gives form to the language.

The syntax of Javascript is very similar to the syntax of other programming languages like Java, C++, C, C#, Python, Ruby, Perl, PHP, etc.


Comments

Comments are used to explain the code. It is not necessary to write comments for the code to work. However, it is good to write comments for the code to be read by other developers. Comments are written in the code using the // for single line comments and /* */ for multi-line comments.

The following is an example of a single line comment.

// This is a single line comment

The following is an example of a multi-line comment.

/* This is a multi-line comment
   * This is the first line
   * This is the second line
   * This is the third line
   */

Variables

Variables are used to store values in the programs. Variables are created using the var keyword in javascript.

The var keyword is followed by the variable name and the variable value. The variable value can be a string, number, boolean, or object.

The following is an example of a variable.

var name;
var age;
var isMarried;

You can also define multiple variables in a single line.

var name, age, isMarried;

Assigning values

Assigning values to variables is done using the = operator. The = operator is followed by the variable name and the variable value. The variable value can be a string, number, boolean, or object.

The following is an example of assigning values to variables.

name = "John";
age = 25;
isMarried = true;

Operators

Operators are used to performing operations on the values of variables. Like addition, subtraction, multiplication, division, and modulus.

The + operator is used to add two numbers. The - operator is used to subtract two numbers. The * operator is used to multiply two numbers. The / operator is used to divide two numbers. The % operator is used to find the remainder of a division.

The following is an example of adding two numbers.

var a = 10;
var b = 20;
var sum = a + b;
var sub = a - b;
var mul = a * b;
var div = a / b;
var mod = a % b;

Conditionals

Conditionals are used to perform different actions based on the value of a variable. The 'if' keyword is followed by the variable name and the variable value. The 'if' keyword is followed by the condition and the action. The condition is followed by the { and the action is followed by the }.

The following is an example of a conditional.

Example

var positiveNumber = true;

if (positiveNumber) {
  console.log("The number is positive");
} else {
  console.log("The number is negative");
}

Loops

Loops are used to perform the same action multiple times.

We have different types of loops in javascript. Here we will look at the syntax of 'for' loop.

The 'for' keyword is followed by the variable name and the variable value. The 'for' keyword is followed by the condition and the action. The condition is followed by the { and the action is followed by the }.

The following is an example of a 'for' loop.

Example

for (var i = 0; i < 10; i++) {
  console.log(i);
}

Functions

Functions are used to perform different actions. Functions are created using the function keyword followed by the function name and the function body. The function body is written using the { and the }.

The following is an example of a function.

Example

function sum(a, b) {
  return a + b;
}
console.log(sum(10, 20));

Objects

Objects are used to store values in the programs. Objects are created using the { and the } keywords.

The { keyword is followed by the object name and the object value. The object value can be a string, number, boolean, or object.

The following is an example of an object.

var person = {
  name: "John",
  age: 25
};

Whitespace and Semicolon in Javascript

Space, tabs, and newlines are considered as whitespace. Unlike other languages, javascript has the ASI (Automatic Semicolon Insertion) technique, which automatically inserts a semicolon at the end of the line.

But it is always recommended to end the line with a semicolon because:


Frequently Asked Questions

  1. How do I get the value of a variable?

    You can use the name of a defined variable to access its value.

  2. How do I set the value of a variable?

    You can use the name of a defined variable to assign a value to it using the = operator.

  3. What does => mean in JS?

    The symbol => is used in arrow functions. It is used to assign the return value of the function to the variable.