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.
1. Variables
Used to store data values.
Declared using var, let, or const.
var x = 10;
let y = 20;
const name = "John";
2. Semicolons
It is used to terminate the statement. Semicolons are often optional.
let x = 5;
let y = 10;
3. 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)
4. 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
5. 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.
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.
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?