JavaScript Syntax
Last Updated : 23 Jan 2026
Introduction
JavaScript syntax dictates the rules of writing valid code similar to C and Java. In simple words, JavaScript syntax refers to the rules and conventions dictating how code is structured and arranged within the JavaScript programming language. It is case sensitive, uses semicolons to terminate statements, and employs curly braces to define code blocks.
Syntax
The syntax of JavaScript is as follows:
JavaScript Values
The JavaScript syntax defines two types of values:
- Fixed values: The fixed values are known as literals.
- Variable values: The variable values are called variables.
JavaScript Literals
JavaScript syntax rules for fixed values are:
In JavaScript, numbers can be written with or without decimals.
Output:
In JavaScript, strings are text that can be written with single or double quotes.
Output:
JavaScript Variables
A JavaScript variable is a named storage location that holds a value, which can be any data type, such as numbers, strings, objects, etc. It can be declared using keywords like var, let, or const.
There are two types of variables in JavaScript, such as:
Local variable: In JavaScript, local variables are declared inside a block or a function.
Global variable: In JavaScript, global variables are declared outside a function or with a window object.
Output:
JavaScript Operators
JavaScript operators are symbols that operate on one or more operands to produce a result. Arithmetic operators (+,-, *, /) are used to compute the value, and Assignment operators (=) are used to assign the values to variables.
Output:
JavaScript Expression
In JavaScript, an expression is a combination of values, operators, and variables. JavaScript expressions are used to compute the values.
Output:
JavaScript Keywords
JavaScript contains multiple keywords that can be used for a particular task. For example, the function keywords can be used to define the function. You can use let, var, and const keywords to define the variables.
Output:
The product of 5 and 10 is 50
JavaScript Comments
In JavaScript, comments are ignored by the JavaScript compiler. Comments increase the readability of code. It adds suggestions, information, and warnings about code.
There are two primary types of codes in JavaScript:
Single-line comments: These comments begin with // and continue to the end of the line.
Multi-line comments: These comments are enclosed between /* and */ and can span multiple lines.
JavaScript Data Types
In JavaScript, we have different data types that hold different values or variables. JavaScript is a dynamic programming language, which means we do not need to specify the type of variable.
There are two types of data types present in JavaScript:
- Primitive data types
- Non-primitive data types
Example
JavaScript Functions
In JavaScript, a function is a reusable block of code used to perform certain operations. The JavaScript function is executed when something calls it.
Syntax
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, Camel case is preferred to name an identifier.
Example
JavaScript Character Set
JavaScript contains a set of Unicode characters. The Unicode characters add special characters like emoji, symbols, etc., to the text. Unicode covers almost all the characters, punctuations, and symbols in the world.