JavaScript Syntax

Introduction

The syntax of JavaScript establishes the guidelines for crafting valid code, akin to those in C and Java. To put it plainly, JavaScript syntax encompasses the rules and conventions that govern the structure and organization of code within the JavaScript programming language. It is case-sensitive, utilizes semicolons to conclude statements, and makes use of curly braces to delineate blocks of code.

Syntax

The syntax of JavaScript is as follows:

Example

console.log("Welcome to our tutorial");

JavaScript Values

The syntax of JavaScript categorizes values into two distinct types:

  • Fixed values: These fixed values are referred to as literals.
  • Variable values: The values that can change are known as variables.
  • JavaScript Literals

JavaScript syntax rules for fixed values are:

In JavaScript, numerical values can be represented either as whole numbers or as decimals.

Example

Example

let a = 10

let b = 11.05

console.log(a);

console.log(b);

Output:

Output

10

11.05

In JavaScript, strings represent sequences of characters that can be enclosed within either single quotes or double quotes.

Example

Example

let str = 'Example'

let str1 = 'Tech'

console.log(str);

console.log(str1);

Output:

Output

Example

Tech

JavaScript Variables

In JavaScript, a variable serves as a designated storage area that contains a value, which can represent various data types, including numbers, strings, objects, and more. Variables can be initialized using keywords such as var, let, or const.

In JavaScript, variables can be classified into two categories, specifically:

Local variable: In JavaScript, local variables are defined within a function or a specific block of code.

Global variable: In JavaScript, global variables can be defined either outside of any function or by utilizing the window object.

Example

Example

let word = "Example";

function myFunction(){

    let a = 20;

    console.log(word);

    console.log(a);

}

myFunction();

Output:

Output

Example

20

JavaScript Operators

In JavaScript, operators are special symbols that perform operations on one or multiple operands in order to generate a result. Arithmetic operators such as (+, -, *, /) serve the purpose of calculating values, while Assignment operators, represented by (=), are utilized to allocate values to variables.

Example

Example

let a, b, div;

a = 10;

b = 5;

div = a/b;

console.log(div);

Output:

JavaScript Expression

In JavaScript, an expression consists of a combination of variables, values, and operators. These expressions in JavaScript are employed to evaluate and derive values.

Example

Example

let a, b, sub, div;

a = 10;

b = 5;

//expression to subtract two numbers

sub = a - b;	

//expression to divide a number

div = a/b;

console.log(sub);

console.log(div);

Output:

JavaScript Keywords

JavaScript encompasses a variety of keywords that serve specific purposes. For instance, the keyword function is utilized for defining functions. Additionally, the keywords let, var, and const are employed for declaring variables.

Example

Example

function getSum(a,b){

    var sum = a * b;

    console.log("The product of " + a + " and " + b + " is "+ sum);

}

let a = 5;

let b = 10;

getSum(a, b);

Output:

Output

The product of 5 and 10 is 50

JavaScript Comments

In JavaScript, the compiler disregards comments entirely. These annotations enhance the clarity of the code. They provide insights, recommendations, and cautions regarding the codebase.

In JavaScript, there are two fundamental categories of code:

Single-line comments: These types of comments start with // and extend to the conclusion of the line.

Example

//This is a single-line comment

let a = 5; //initialize a to 5

Multi-line comments: These comments are encapsulated within / and / and have the capability to extend across several lines.

Example

/* This is a multi-line comment

  Welcome to our tutorial */

let a = 5;

JavaScript Data Types

In JavaScript, there exists a variety of data types that can store different values or variables. As a dynamically typed programming language, JavaScript allows us to declare variables without the necessity of specifying their data type.

JavaScript encompasses two categories of data types:

  • Primitive data types
  • Non-primitive data types
  • Example

    Example
    
    // it stores a string data type
    
    let str = "Example";
    
    //it stores an integer data type
    
    let a = 10;
    
    //it stores Boolean data type
    
    (a == b)
    
    // it stores array data type
    
    let fruits = ["Apple", "Orange", "Banana"];
    
    //it store object data 
    
    let study = {
    
        firstName: "Rex",
    
        lastName: "Davies",
    
        age: 24,
    
        mark: "redEye"
    
    }
    

    JavaScript Functions

In JavaScript, a function represents a reusable segment of code designed to carry out specific tasks. The execution of a JavaScript function occurs when it is invoked by a particular call.

Syntax

Example

function functionName(parameters){

    //code to be executed

}

Example

Example

function myDemo(){

    let a = 9;

    let b = 1;

    let sum = a + b;

    console.log(sum);

}

myDemo();

Output:

JavaScript Identifier

JavaScript identifiers are names that are used to name variables and keywords, or functions. In JavaScript, an identifier must begin with:

  • A letter (A-Z to a-z)
  • A dollar sign($)
  • A underscore(_)
  • JavaScript Camel Case

In JavaScript, the naming convention known as Camel case is commonly utilized for defining identifiers.

Example

Example

let firstName

let rollNo

JavaScript Character Set

JavaScript incorporates a collection of Unicode characters. These Unicode characters enable the inclusion of special elements such as emojis, symbols, and more within the text. Unicode encompasses nearly all characters, punctuation marks, and symbols found globally.

Input Required

This code uses input(). Please provide values below: