JavaScript Syntax

In this tutorial, you will learn JavaScript syntax. It has basically a set of rules that define how a JavaScript program is written and interpreted by the browser or JavaScript engine.

It includes how variables are declared, how functions are written, how conditions are applied, and how statements are structured.

Basic Structure of JavaScript Code

Example:


let message = "Hello, JavaScript!";
console.log(message);

Output:

Hello, JavaScript!

Explanation:

  1. let message = “Hello, JavaScript!”; is a variable declaration
  2. console.log(message); is a output statement

JavaScript Variables

It is used to store data values.

Way to declare using var, let, or const.


var x = 10;
let y = 20;
const name = "John";

Rules:

  1. Variable names are case-sensitive
  2. Cannot start with numbers
  3. Use meaningful names

Semicolons in JavaScript

It is used to terminate the statement. Semicolons are often optional.


let x = 5;
let y = 10;

Semicolons are optional in many cases, but it is recommended to use them to avoid unexpected errors.

JavaScript Data Types

JavaScript supports different types of data:

Primitive Types:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • BigInt
  • Symbol

Example:


let name = "John";   // String
let age = 35;         // Number
let isActive = true;  // Boolean

Automatic Type Conversion (Coercion)

JavaScript automatically converts data types when performing operations between different types (e.g., adding a number to a string).


let result = 5 + "5"; // "55" (number 5 is converted to string)

Identifiers

Identifiers are names given to variables, functions, and objects. They must start with a letter, underscore (_), or dollar sign ($), followed by letters, digits, underscores, or dollar signs.


let name = "John";
let $numValue = 100;
let _isUserAllowed = true;

Note: We can’t declare a variable that starts with a number.


let 1name = "John";  // not allowd

Reassigning Variables

Variables declared with var or let can be reassigned.

Variables declared with const cannot be reassigned.


let a = 10;
a = 20; // Valid

const b = 50;
// b = 100; // Error: Assignment to constant variable.

Common Mistakes in JavaScript Syntax

You will some common mistakes which are mostly happend by Beginners.

1. Missing Semicolons

Some times Beginners forget semicolon after assign the value in the variable.


let a = 10
let b = 20

2. Case Sensitivity

Variable is case sensitive.


let name = "John";
console.log(Name); // Error

In the above example, when you print the value then you will get error.

3. Using = Instead of == or ===

When you want to compare the value then you should use == or ===.


if (x = 10) { } // Wrong

You should use == or === to compare the value


if (x === 10) { } // correct

4. Not Declaring Variables


x = 10; // Bad practice

Correct way


let x = 10; 

JavaScript Best Practices to declare a Variable

You will see some best practice to declare a variable.

  1. Use let and const instead of var
  2. Write clean and readable code
  3. Use meaningful variable names
  4. Comment your code

JavaScript Syntax – Interview Questions

Q 1: What is JavaScript syntax?
Ans: It is the set of rules used to write JavaScript code.
Q 2: Is JavaScript case-sensitive?
Ans: Yes, JavaScript is case-sensitive.
Q 3: How are statements separated?
Ans: Using semicolons (;).
Q 4: How do you write a JavaScript block?
Ans: Using curly braces { }.
Q 5: How do you declare variables in JavaScript?
Ans: Using var, let, or const.

Conclusion

JavaScript syntax is the foundation of writing effective and error-free code. By understanding its rules, you can build powerful applications.

JavaScript Syntax – Objective Questions (MCQs)

Q1. JavaScript statements are ended with ______.






Q2. Which symbol is used for code blocks?






Q3. JavaScript is a ______ sensitive language.






Q4. Which syntax is correct?






Q5. Which symbol is used for assignment?