JavaScript

Syntax of JavaScript

The syntax of JavaScript refers to the set of rules that helps to construct a program in JavaScript. In this article, we will describe the basic syntax of JavaScript and will also discuss some basic rules related to the syntax of JavaScrip. Following are some basic rules for the syntax of JavaScript:

  • it is a case sensitive language.
  • every statement ends with a semicolon (;).
  • variables can be started from an alphabetical digit or an underscore (_).

JavaScript Values

There are two types of values defined in JavaScript:

  • variable Values
  • fixed Values

Variable Values

Variable values are the variable containers that are used to store the data values. In JavaScript, some specific keywords like let, const, and var are used to declare the variables.

For example, a variable “x” is defined below:

let x;

x= 6:

Types of variables

There are two types of variables in JavaScript:

Global variables: Global variables are those which we can access from anywhere in the code. These variables are declared from the outside of the function.

Local variables: Local variables are those which are declared inside a function and cannot be accessed from outside of the function.

// Global variable declaration
var name="Alice";  

// Function definition
functionnewFunction() {

// Local variable declaration
var num = 45;  

// Display the value of Global variable
document.writeln(Name);
// Display the value of local variable
document.writeln("<br>" + num );
}
// Function call
newFunction();

Fixed Values

Fixed values are known as literals or constant values.

There are two types of literals in JavaScript:

Numbers: Numbers are the numeric values with or without decimals. The syntax of numbers is given below.

7.2

28908

Strings: Strings are in text form; this text is written within quotes. The syntax of the string is given below.

"Mark Alice"

'Mark Alice'

JavaScript Operators

In JavaScript, arithmetic operators (‘+,’ ‘-,’ ‘*,’ ‘/’) are used to implement some operations on different operands. The assignment operators (=, %=, +=) are also used. One example is given below based on an arithmetic operator:

// Declaration of variables
var a, b, sum;
// Assign value to the variables
a= 32;
b = 25;
// Use arithmetic operator to
// add both numbers
sum = a + b;
document.write(sum);

Data Types in JavaScript

There are different data types in JavaScript because it holds various types of variables. It is a dynamic programming language; therefore, variable specification is not necessary.

JavaScript use two types of data types:

  • Primitive data type: In JavaScript, primitive data is immutable data, which cannot be altered. For example, numbers, strings, boolean numbers, etc.
  • Non-primitive data type: In JavaScript, non-primitive data types are objects and methods such as functions and arrays are non-primitive data types.

JavaScript Functions

In JavaScript, functions are the code blocks that are used to perform some specific tasks or operations. These functions are reusable and can reduce the computational cost of a program.

Syntax of a function is given below:

// Definition of function
functionfunc() {
// Declare a variable
var num = 15;                
// Show the result
document.writeln(num);    
}
// Function invoke
func();

JavaScript Keywords

In JavaScript, there are some reserved words with special meanings. Some keywords are given in example:

// var keyword is used to define the variable

var a, b;

// function is the keyword that instruct the browser to create a function

function aBc(){};

JavaScript Comments

Comments are part of the code and are ignored during the execution.. We use comments to add the information and suggestions in the code. They increase the readability of code and make the code user-friendly.

In JavaScript, any line between the /* and */ and after double // is considered a comment and not considered during execution.

In the given example the syntax of comments is given:

let x = 7; // It will be executed

// x = 5; It will NOT be executed

JavaScript Name Identifiers

In JavaScript, identifiers are used to identify the variable names, functions, and keywords.

These names must start with:

  • a dollar sign ($)
  • an underscore (_)
  • or any letter from the alphabet.

However, numbers at the start of names are not accepted in JavaScript.

Conclusion

JavaScript is a dynamic programming language, which has some defined rules and syntax. To work with JavaScript we should learn the basic concepts of JavaScript such as data types, keywords, functions, name identifiers, etc. In this tutorial, all basic rules and functions of JavaScripts are defined with proper code examples.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.