📘 Lesson  ·  Lesson 03

JavaScript Syntax

JavaScript Syntax

Statements and Semicolons

let name = "Aman";      // statement 1
let age = 20;           // statement 2
console.log(name);      // statement 3
A JavaScript program is a series of statements — instructions the browser executes one by one, top to bottom. Each statement usually does one thing: declare a variable, call a function, perform a calculation. Statements typically end with a semicolon ;, which marks where one instruction finishes. JavaScript can often figure out statement endings on its own (called "automatic semicolon insertion"), so semicolons are technically optional in many cases — but using them consistently is a good habit that prevents subtle bugs. As a beginner, end your statements with semicolons.

Case Sensitivity — a critical rule

let myName = "Aman";
console.log(myname);   // ERROR! "myname" is NOT the same as "myName"

let Age = 20;
let age = 25;          // these are TWO DIFFERENT variables
JavaScript is case-sensitive — this trips up beginners constantly. myName, myname, MyName, and MYNAME are four completely different things to JavaScript. If you declare userName but later type username, JavaScript sees an unknown variable and errors out. This applies to everything: variable names, function names, and built-in keywords. console.log works, but Console.log or console.Log will fail. Being consistent and careful with capitalisation is essential — a single wrong letter case is one of the most common sources of "why isn't my code working?" frustration.

Whitespace and Code Blocks

// These two lines do the exact same thing:
let x=5;
let x = 5;              // spaces around = are ignored, just easier to read

// Curly braces { } group statements into a BLOCK:
if (age >= 18) {
    console.log("Adult");
    console.log("Can vote");
}
JavaScript mostly ignores extra spaces and blank lines — they don't change how code runs, but they make it far more readable. Good developers use spacing and indentation generously so code is easy to follow (like let x = 5 instead of let x=5). Curly braces { } group multiple statements into a "block" — a set of statements that belong together, such as the code inside an if or a function. Indenting the code inside braces (usually 2 or 4 spaces) is a universal convention that shows the structure at a glance. Whitespace is for humans; use it to keep your code clean.

Identifiers and Naming Rules

// Valid names:
let firstName;
let user_age;
let total2;
let $price;

// INVALID names:
let 2fast;      // can't START with a number
let my-name;    // no hyphens allowed
let user name;  // no spaces allowed
An "identifier" is the name you give to variables and functions. The rules: a name can contain letters, digits, underscores _ and dollar signs $, but it must NOT start with a digit, contain spaces, or use hyphens. So score1 is fine but 1score is not; user_name works but user-name does not. The universal convention in JavaScript is camelCase: start with a lowercase letter and capitalise each new word — firstName, totalPrice, isLoggedIn. This is the standard style you should follow throughout. Choose descriptive names (userAge not x) so your code explains itself.

Keywords — reserved words

// These are RESERVED - you can't use them as your own names:
let, const, var, if, else, for, while, function,
return, true, false, null, class, new, this ...

let if = 5;      // ERROR - "if" is a keyword, can't be a variable name
JavaScript has a set of keywords — special words that the language reserves for its own use, like let, if, function, and return. Because these have built-in meanings, you can't use them as your own variable or function names (naming a variable if or return causes an error). You don't need to memorise the full list — you'll learn each keyword as you use it, and your code editor will highlight them in a distinct colour, making them easy to recognise. Just be aware they're off-limits as custom names.

Exam Corner

Q: What ends a JavaScript statement? A semicolon ; (technically optional in many cases, but recommended).

Q: Is JavaScript case-sensitive? Yes — myVar and myvar are different things.

Q: What do curly braces { } do? Group statements into a block that belong together (as in if statements and functions).

Q: What is the standard naming convention in JavaScript? camelCase — e.g. firstName, totalPrice.

Q: Can a variable name start with a number? No — names can't start with a digit, and can't contain spaces or hyphens.

Statements और Semicolons

let name = "Aman";      // statement 1
let age = 20;           // statement 2
console.log(name);      // statement 3
JavaScript program statements की श्रृंखला है — instructions जो browser एक-एक करके, ऊपर से नीचे execute करता है. हर statement आमतौर पर एक काम करता है: variable declare करना, function call करना, calculation करना. Statements आमतौर पर semicolon ; से खत्म होते हैं, जो बताता है एक instruction कहां खत्म हुआ. JavaScript अक्सर statement endings खुद पता लगा सकता है ("automatic semicolon insertion" कहलाता है), तो semicolons तकनीकी रूप से कई मामलों में optional हैं — पर उन्हें consistently use करना अच्छी आदत है जो subtle bugs रोकती है. Beginner के रूप में, अपने statements semicolons से खत्म कीजिए.

Case Sensitivity — अहम rule

let myName = "Aman";
console.log(myname);   // ERROR! "myname" "myName" ke SAMAAN nahi hai

let Age = 20;
let age = 25;          // ye DO ALAG variables hain
JavaScript case-sensitive है — यह beginners को लगातार उलझाता है. myName, myname, MyName, और MYNAME JavaScript के लिए चार बिल्कुल अलग चीज़ें हैं. अगर आप userName declare करें पर बाद में username type करें, JavaScript एक unknown variable देखता है और error देता है. यह हर चीज़ पर लागू होता है: variable names, function names, और built-in keywords. console.log चलता है, पर Console.log या console.Log fail होगा. Capitalisation के साथ consistent और सावधान रहना ज़रूरी है — एक गलत letter case "मेरा code काम क्यों नहीं कर रहा?" परेशानी के सबसे common स्रोतों में से एक है.

Whitespace और Code Blocks

// Ye do lines bilkul ek jaisa kaam karti hain:
let x=5;
let x = 5;              // = ke aas-paas spaces ignore, bas padhne me aasan

// Curly braces { } statements ko BLOCK me group karte hain:
if (age >= 18) {
    console.log("Adult");
    console.log("Can vote");
}
JavaScript ज़्यादातर extra spaces और blank lines ignore करता है — वे code कैसे चलता है नहीं बदलते, पर उसे कहीं ज़्यादा readable बनाते हैं. अच्छे developers spacing और indentation उदारता से use करते हैं ताकि code follow करना आसान हो (जैसे let x=5 के बजाय let x = 5). Curly braces { } कई statements को "block" में group करते हैं — साथ रहने वाले statements का समूह, जैसे if या function के अंदर का code. Braces के अंदर code indent करना (आमतौर पर 2 या 4 spaces) universal convention है जो structure एक नज़र में दिखाता है. Whitespace इंसानों के लिए है; इसे अपना code साफ रखने को use कीजिए.

Identifiers और Naming Rules

// Valid names:
let firstName;
let user_age;
let total2;
let $price;

// INVALID names:
let 2fast;      // number se SHURU nahi ho sakta
let my-name;    // hyphens allowed nahi
let user name;  // spaces allowed nahi
"Identifier" वह नाम है जो आप variables और functions को देते हैं. Rules: नाम में letters, digits, underscores _ और dollar signs $ हो सकते हैं, पर यह digit से शुरू NAHI होना चाहिए, spaces नहीं, या hyphens नहीं. तो score1 ठीक है पर 1score नहीं; user_name चलता है पर user-name नहीं. JavaScript में universal convention camelCase है: lowercase letter से शुरू करके हर नए शब्द को capital कीजिए — firstName, totalPrice, isLoggedIn. यह standard style है जो आपको हमेशा follow करनी चाहिए. Descriptive नाम चुनिए (x नहीं userAge) ताकि आपका code खुद को समझाए.

Keywords — reserved words

// Ye RESERVED hain - inhe apne naam ke roop me use nahi kar sakte:
let, const, var, if, else, for, while, function,
return, true, false, null, class, new, this ...

let if = 5;      // ERROR - "if" keyword hai, variable naam nahi ban sakta
JavaScript में keywords का समूह है — खास शब्द जो language अपने इस्तेमाल के लिए reserve करती है, जैसे let, if, function, और return. क्योंकि इनके built-in अर्थ हैं, आप इन्हें अपने variable या function names के रूप में use नहीं कर सकते (variable को if या return नाम देना error देता है). आपको पूरी list याद करने की ज़रूरत नहीं — आप हर keyword उसे use करते समय सीखेंगे, और आपका code editor उन्हें अलग रंग में highlight करेगा, पहचानना आसान बनाते. बस जान लीजिए वे custom names के रूप में off-limits हैं.

Exam Corner

Q: JavaScript statement को क्या खत्म करता है? Semicolon ; (कई मामलों में तकनीकी रूप से optional, पर recommended).

Q: क्या JavaScript case-sensitive है? हां — myVar और myvar अलग चीज़ें हैं.

Q: Curly braces { } क्या करते हैं? साथ रहने वाले statements को block में group करते हैं (जैसे if statements और functions में).

Q: JavaScript में standard naming convention क्या है? camelCase — जैसे firstName, totalPrice.

Q: क्या variable नाम number से शुरू हो सकता है? नहीं — नाम digit से शुरू नहीं हो सकते, और spaces या hyphens नहीं रख सकते.
← Back to JavaScript Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.