The JavaScript syntax is the most popular and widely used variant of the ECMAScript language. It’s a set of rules that dictate how the language is interpreted and written by the browser (by the programmer).
The JavaScript syntax is loosely based on the Java language. Java is a complete programming environment, while JavaScript may be considered a subset of it. That being said, where their similarities begin and end is different — Java and JavaScript are two distinct animals.
You’ll come across terms like variables, functions, statements, operators, data types, objects, and so on while learning JavaScript.
It would take a long time to cover the whole JavaScript syntax in a single lesson. The syntax fundamentals covered in this lesson, on the other hand, will allow you to code JavaScript effectively.
JavaScript Variables
The name of a variable in JavaScript is the simple name for the storage space where data will be kept. In JavaScript, there are two sorts of variables:
- Local variables: Declare a variable in a block or function.
- Global variables: Outside of a function, declare a variable with the window object.
JavaScript Operator
The distinction between assignment and addition is not always clear, but it’s a crucial one. JavaScript operators are symbols that may be used to operate on arguments. The value of a calculation is computed with arithmetic operators ( + , -, *, / ), whereas assignment operators ( =, +=, %= ) are used to assign the results to variables.
JavaScript Expression
The expression is the union of values, operators, and variables. It’s used to calculate the results.
JavaScript Keyword
Reserved words in JavaScript have special meanings and are called keywords.
JavaScript Comments
The comments are always ignored by the JavaScript compiler. It makes code more readable. It provides code advice, information, and warnings. Everything inside double slashes // (single line comment) or between /* and */ (multi-line comment) is considered a remark and ignored by the JavaScript compiler.
JavaScript Data Types
JavaScript offers numerous data types to store various values on variables. Because JavaScript is a dynamic programming language, it does not need you to specify the type of variable. In JavaScript, there are two basic sorts of data types.
- Primitive data type
- Non-primitive (reference) data type
JavaScript Functions
JavaScript functions are pieces of code that may be used to do specific tasks. When something invokes a JavaScript function, it is executed. It is called many times, therefore the function can be reused.
JavaScript Identifiers
Many variables, parameters, functions, classes, and other elements need names. A name for a variable, parameter, function, class, or other items should begin with a letter (a-z or A-Z), an underscore (_), or a dollar sign ($) and contain one or more characters that are separated by underscores (_).
The letter is not limited to the ASCII character, and it may include extended ASCII or Unicode characters, which are not recommended. Identifiers are case-sensitive. The message differs from The Message, for example.
JavaScript Statements
A statement is a code that denounces a variable or tells the JavaScript engine to perform an activity. A basic statement is ended with a semicolon (;). Although the semicolon (;) is not required, you should always use it to conclude a statement.
JavaScript Whitespace
Spaces are used to separate characters. JavaScript has the following whitespace:
- Space
- Carriage return
- tab
- New Line
JavaScript has no whitespace emphasis. However, you may utilize whitespaces to make the code easier to read and maintain by formatting it.
JavaScript files, however, are usually not compressed. You should note that remove all whitespace JavaScript bundlers from the JavaScript files and consolidate them into a single file for distribution. This makes the JavaScript code lighter and quicker to load in web browsers as a result of which it is ideal for on-the-fly code development.
Camel Case And JavaScript
Traditionally, three methods of combining several terms into 1 variable name have been used:
Underscore: first_name, master_card, last_name, inter_city.
Hyphens: first-name, master-card, last-name, inter-city.
Camel Case: MasterCard, LastName, InterCity, FirstName
In programming languages, particularly in JavaScript, the camel case frequently begins with a lowercase letter: lastName, firstName, interCity, Mastercard
JavaScript Semicolons
In JavaScript, semicolons are optional. However, I recommend including them all the time because otherwise, JavaScript may get it wrong about the end of a statement. Automatic Semicolon Insertion explains how to do so.
Semicolons are not used to terminate blocks. A function expression is an expression that ends with a block, and there is one situation in which you’ll see a semicolon following a block: when a function expression follows a statement. If such an expression appears last in a statement, it is followed by a semicolon.
JavaScript Booleans
The boolean data type has two values: true and false. The following Boolean operators are available:
- Comparison operators:
- Prefix logical operator: ! (Not)
- Binary logical operators: && (And), || (Or)
- Ordering operators (for strings and numbers): >, >=, <, <=
- Equality operators: ===, !==, ==, !=
Truthy and Falsy
Any value can be used in the context of a boolean in JavaScript, and it will be interpreted as either false or true. The following values are treated as false:
- Number: 0, NaN
- undefined, null
- String: ”
- Boolean: false
If the condition evaluates to false, all expressions in the if statement is evaluated. All other values (including all objects!) are deemed to be true. Falsy and truthy values are terms that refer to values interpreted as false or true, respectively. Boolean(), a function that turns its input into a boolean, is called.
Strings
Strings can be constructed simply with a string literal. Single or double quotes surround the literal text. The backslash (\) escapes characters and generates a number of control codes.
String Operators
Strings are joined via the plus (+) operator, which turns the other argument into a string if one of the arguments is a string.
Loops
The for loop has the following syntax:
for (⟦«init»⟧; ⟦«condition»⟧; ⟦«post_iteration»⟧)
«statement»
These functions are not included in the loop condition. Before each loop iteration, the condition is evaluated; if it turns out to be false, the loop is stopped. After each loop around, post_iteration is run.
The array example displays all of the elements of arr on the console.
for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}
The while loop continues to iterate over its body as long as the condition is true:
// Same as for loop above:
var i = 0;
while (i < arr.length) {
console.log(arr[i]);
i++;
}
The do-while loop repeats while its condition is true. The body is always executed at least once, as the condition follows the body:
do {
// …
} while (condition);
In every loop:
- break leaves the loop
- continue starts a new loop iteration

Brian Taylor is a JavaScript developer and educator, dedicated to demystifying programming for newcomers. With a career spanning over a decade in web development, Brian has a deep understanding of JavaScript and its ecosystem. He is passionate about teaching and has helped countless beginners grasp the fundamentals of JavaScript, enabling them to build their own web applications.



