GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Local variables:
  • Introduction3 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Syntax - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Syntax

Last Updated : 11 Jul, 2025

JavaScript syntax refers to the rules and conventions dictating how code is structured and arranged within the JavaScript programming language. This includes statements, expressions, variables, functions, operators, and control flow constructs.

Syntax

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

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

// Variable declaration
let c, d, e;

// Assign value to the variable
c = 5; 

// Computer value of variables
d = c;
e = c / d;

JavaScript Values

There are two types of values defined in JavaScript Syntax:

  • Fixed Values: These are known as the literals.
  • Variable values: These are called variables

These are the features of JavaScript which have some predefined syntax:

Table of Content

  • JavaScript Literals
  • JavaScript Variables
  • JavaScript Operators
  • JavaScript Expressions
  • JavaScript Keywords
  • JavaScript Comments
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Identifiers

JavaScript Literals

Syntax Rules for the JavaScript fixed values are:

  • JavaScript Numbers can be written with or without decimals.
  • Javascript Strings are text that can be written in single or double quotes.
JavaScript
let num1 = 50
let num2 = 50.05

let str1 = "Geek"
let str2 = 'Geeks'

console.log(num1)
console.log(num2)
console.log(str1)
console.log(str2)

Output
50
50.05
Geek
Geeks

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: This example shows the use of JavaScript variables.

JavaScript
// 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);
}

// Function call
MyFunction();

Output: 

Apple
45

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: This example shows the use of javascript operators.

JavaScript
// 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);

Output
26

JavaScript Expressions

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

Example: This example shows a JavaScript expression.

JavaScript
// 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 + "\n" + sum);

Output
10
50

JavaScript Keywords

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

// 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: This example shows the use of javascript comments.

JavaScript
// 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);

Output
50

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
// It store string data type
let txt = "GeeksforGeeks";

// It store integer data type
let a = 5;
let b = 5;

// 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", "Hello"];

// It store object data (objects are 
// represented in the below way mainly)
let Student = {
    firstName: "Johnny",
    lastName: "Diaz", 
    age: 35, 
    mark: "blueEYE"
}

JavaScript Functions

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.

Syntax: 

function functionName( par1, par2, ....., parn ) {  
    // Function code
}  

The JavaScript function can contain zero or more arguments.

Example: This example shows the use of Javascript functions.

JavaScript
// Function definition
function func() {

    // Declare a variable
    let num = 45;

    // Display the result
    console.log(num);
}

// Function call
func();

Output
45

JavaScript Identifiers

JavaScript Identifiers are names used to name variables and keywords and functions.

A identifier must begin with:

  • A letter(A-Z or a-z)
  • A dollar sign($)
  • A underscore(_)

Note: Numbers are not allowed as a first character in JavaScript Identifiers.

JavaScript Case Sensitive

JavaScript Identifiers are case-sensitive.

Example: Both the variables firstName and firstname are different from each other.

JavaScript
let firstName = "Geek";
let firstname = 100;

console.log(firstName);
console.log(firstname);

Output
Geek
100

JavaScript Camel Case

In JavaScript Camel case is preferred to name a identifier.

Example:

let firstName
let lastName

JavaScript Character Set

A unicode character set is used in JavaScript. A unicode covers the characters, punctuations and symbols.

We have a complete article on character sets. Click here to read Charsets article.

Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.