HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Syntax

JavaScript Syntax is used to define the set of rules to construct a JavaScript code. You need to follow all these so that you can work with JavaScript.

Example

console.log("Basic Print method in JavaScript");
              

JavaScript syntax refers to the set of rules that determines how JavaScript programs are constructed:

Example

// How to create variables:
var x;
let y;

// How to use variables:
x = 5;
y = 6;
let z = x + y;

JavaScript Variables

A JavaScript variable is the simple name of the storage location where data is stored. There are two types of variables in JavaScript which are listed below:

  • Local variables: Declare a variable inside of a block or function.
  • Global variables: Declare a variable outside function or with a window object.

Example

// Declare a variable and initialize it
// Global variable declaration
let Name = "Apple";
 
// Function definition
function MyFunction() {
 
    // Local variable declaration
    let num = 45;
 
    // Display the value of Global variable
    console.log(Name);
 
    // Display the value of local variable
    console.log(num);
}
 

JavaScript Operators

JavaScript operators are symbols that are used to compute the value or in other words, we can perform operations on operands. Arithmetic operators ( +, -, *, / ) are used to compute the value, and Assignment operators ( =, +=, %= ) are used to assign the values to variables.

Example

// Variable Declarations
let x, y, sum;
 
// Assign value to the variables
x = 3;
y = 23;
 
// Use arithmetic operator to
// add two numbers
sum = x + y;
 
console.log(sum);

JavaScript Expression

Expression is the combination of values, operators, and variables. It is used to compute the values.

Example

// Variable Declarations
let x, num, sum;
 
// Assign value to the variables
x = 20;
y = 30
 
// Expression to divide a number
num = x / 2;
 
// Expression to add two numbers
sum = x + y;
 
console.log(num + "<br>" + sum);
  

JavaScript Keyword

The keywords are the reserved words that have special meanings in JavaScript.

Example

// let is the keyword used to
// define the variable
let a, b;
// function is the keyword which tells
// the browser to create a function
function GFG(){};
 
  

JavaScript Comments

The comments are ignored by the JavaScript compiler. It increases the readability of code. It adds suggestions, Information, and warning of code. Anything written after double slashes // (single-line comment) or between /* and */ (multi-line comment) is treated as a comment and ignored by the JavaScript compiler.

Example

// Variable Declarations
let x, num, sum;
 
// Assign value to the variables
x = 20;
y = 30
 
/* Expression to add two numbers */
sum = x + y;
 
console.log(sum);
 

JavaScript Data Types

JavaScript provides different datatypes to hold different values on variables. JavaScript is a dynamic programming language, which means do not need to specify the type of variable.
There are two types of data types in JavaScript.

  • Primitive data type
  • Non-primitive (reference) data type

Example

// It store string data type
let txt = "GeeksforGeeks";
// It store integer data type
let a = 7;
let b = 7;
// It store Boolean data type
(a == b )
// To check Strictly (i.e. Whether the datatypes
// of both variables are same) === is used
(a === b)---> returns true to the console
 
// It store array data type
let places= ["GFG", "Computer", "welcome"];
// It store object data (objects are 
// represented in the below way mainly)
let Student = {
firstName:"Sohan",
 lastName:"Kumar", 
 age:35, 
 mark:"mole mark"}

JavaScript FunctionsJavaScript Variables

JavaScript functions are the blocks of code used to perform some particular operations. JavaScript function is executed when something calls it. It calls many times so the function is reusable.

Example

// Function definition
function func() {
 
    // Declare a variable
    let num = 45;
 
    // Display the result
    console.log(num);
}
 
// Function call
func();