JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Syntax


📘 JavaScript Syntax – How to Write JavaScript Properly

JavaScript syntax is a set of rules that define how programs are written. Learning the basic syntax is essential to avoid errors and write clean, readable code.


🔹 JavaScript Statements

A JavaScript statement is a command or instruction that tells the browser to do something.

let x = 5;
console.log(x);

Each line above is a statement. Statements are usually ended with a semicolon ;, although it's optional in many cases.


🔹 JavaScript Keywords

JavaScript has reserved keywords used to perform actions:

Keyword Description
var Declares a variable
let Declares a block-scoped variable
const Declares a constant value
if Conditional statement
for Loop
function Defines a function

🔹 JavaScript Values

JavaScript values are either:

  • Fixed values (literals): 5, "Hello", true

  • Variable values: declared with let, const, or var


🔹 JavaScript Identifiers

Identifiers are the names you give to variables and functions. They must follow these rules:

  • Must begin with a letter, underscore _, or dollar sign $

  • Cannot start with a number

  • Case-sensitive

let name = "John";
let Name = "Doe"; // Different from 'name'

🔹 JavaScript Operators

Used to perform operations:

let x = 5 + 3; // + is the addition operator

🔹 JavaScript Code Blocks

Code blocks are enclosed in curly braces {}. They group multiple statements together, especially in if, for, or function blocks.

if (x > 5) {
  console.log("x is greater than 5");
}

🟡 JavaScript Comments
  • Single-line: // This is a comment

  • Multi-line:

    /*
      This is a
      multi-line comment
    */
    

Practice Questions

Q1. How do you declare a variable city and assign the value "Delhi" using JavaScript syntax?

Q2. What is the correct way to write a single-line comment in JavaScript?

Q3. How do you declare two variables x and y, assign values 10 and 20, and add them together in a new variable sum?

Q4. How can you write a JavaScript block that logs "Good Morning" only if a variable hour is less than 12?

Q5. How do you declare a constant named PI and assign the value 3.14 to it using correct syntax?

Q6. How do you write a multi-line comment in JavaScript explaining what a function does?

Q7. How do you define a function called greet that logs "Welcome!" when called?

Q8. What is the correct way to name a variable that stores a user's age, following JavaScript identifier rules?

Q9. How do you use an operator to subtract 5 from 15 and store the result in a variable named difference?

Q10. How do you write a loop that runs 3 times and logs the index value using proper JavaScript syntax?


JS Syntax Quiz

Q1: Which of the following is a valid JavaScript variable declaration?

A. int x = 10;
B. let x = 10;
C. var = 10;
D. define x = 10;

Q2: What symbol is used to start a single-line comment in JavaScript?

A. <!--
B. #
C. //
D. **

Q3: What are JavaScript identifiers used for?

A. To comment code
B. To store data
C. To name variables and functions
D. To create loops

Q4: Which of the following is NOT a valid identifier in JavaScript?

A. _username
B. $value
C. 1total
D. total1

Q5: What does this JavaScript statement do: let x = 5 + 3;?

A. Declares x and subtracts 3 from 5
B. Adds 5 and 3 and stores the result in x
C. Assigns a string to x
D. Logs x to the console

Q6: What is the purpose of a code block in JavaScript?

A. To hide code
B. To group statements
C. To slow down execution
D. To store styles

Q7: Which character is used to group a block of code in JavaScript?

A. ()
B. []
C. {}
D. <>

Q8: Which of the following is a JavaScript keyword?

A. print
B. output
C. const
D. define

Q9: What happens if you omit the semicolon in JavaScript?

A. The program crashes
B. It adds extra whitespace
C. JavaScript may insert it automatically
D. The browser gives an alert

Q10: Which of the following is a multi-line comment in JavaScript?

A. <!-- This is a comment -->
B. // This is a comment
C. /* This is a comment */
D. ** This is a comment **

Go Back Top