JavaScript

coding learning websites codepractice

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?


JavaScript

online coding class codepractice

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

Go Back Top