← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 4 of 12

JS Statements

Think of a recipe written as a list of steps, crack the eggs, mix the flour, heat the pan, each line is one complete instruction that the cook carries out in order before moving to the next. A JavaScript statement works the same way, it's one complete instruction that tells the browser to do something, like storing a value or displaying a message, and a full JavaScript program is simply a sequence of these statements running one after another. Understanding how statements are separated and grouped is the foundation for reading and writing any JavaScript code, whether it's a tiny script or a full application powering a site like cookiescursor.com.
Syntax
javascript
statement1;
statement2;
let variable = value;

Statement क्या है

Statement एक अकेला निर्देश है जिसे JavaScript engine execute करता है, जैसे variable घोषित करना, function बुलाना या कोई नया value assign करना। JavaScript program असल में बस statements का एक क्रम है जो एक के बाद एक, क्रम से चलता है।

Note: हर statement को एक पूरा विचार मानिए, अगर कोई line एक साफ़ काम करती है, तो वह शायद एक अकेला statement है।
Warning: An expression alone, like just typing a number, is valid but does nothing useful on its own unless it's part of a full statement.

उदाहरण: What Is a Statement

javascript
// Declare the variable `score`, set to `10`
// Declare the variable `score`, set to `10`
let score = 10;
score = score + 5;
// Print `score` to the console
// Print `score` to the console
console.log(score);

Semicolons

Semicolons किसी statement के अंत को चिह्नित करते हैं, और JavaScript का automatic semicolon insertion कई मामलों में उन्हें आपके लिए जोड़ सकता है, फिर भी उन्हें स्पष्ट रूप से लिखना code को ज़्यादा पूर्वानुमेय और पढ़ने में आसान बनाता है।

Note: हर statement को semicolon से खत्म करने की आदत डालिए, यह इस अस्पष्टता को दूर करता है कि एक निर्देश कहाँ खत्म होता है और अगला कहाँ शुरू।
Warning: Automatic semicolon insertion can misbehave around line breaks in certain patterns, such as after a return keyword, silently changing what your code does.

उदाहरण: Semicolons

javascript
// Declare the variable `a`, set to `1`
// Declare the variable `a`, set to `1`
let a = 1;
// Declare the variable `b`, set to `2`
// Declare the variable `b`, set to `2`
let b = 2;
// Print `a + b` to the console
// Print `a + b` to the console
console.log(a + b);

Code Blocks

घुँघराले कोष्ठक { } कई statements को एक अकेले code block में समूहित करते हैं, जो आमतौर पर functions, loops और conditional statements की body परिभाषित करने के लिए इस्तेमाल होते हैं।

कोष्ठकों के अंदर की हर चीज़ एक इकाई मानी जाती है, भले ही उसमें कई अलग-अलग statements हों।

Note: Code blocks के भीतर एकसमान indentation nested logic को अनुसरण करना नाटकीय रूप से आसान बनाता है, भले ही JavaScript खुद उस indentation को अनदेखा करती हो।
Warning: A mismatched or missing closing brace is one of the most common syntax errors beginners encounter, always check that every opening brace has a matching close.

उदाहरण: Code Blocks

javascript
// Check whether `true`
// Check whether `true`
if (true) {
  // Declare the variable `message`, set to "Inside a code block"
  // Declare the variable `message`, set to "Inside a code block"
  let message = "Inside a code block";
  // Print `message` to the console
  // Print `message` to the console
  console.log(message);
}

JavaScript में Whitespace

JavaScript statements के बीच के अतिरिक्त spaces, tabs और खाली lines को काफ़ी हद तक अनदेखा करती है, यानी code को मनुष्यों के पढ़ने के लिए format किया जा सकता है बिना यह बदले कि वह कैसे चलता है।

इसीलिए एकसमान indentation शैली की एक पसंद है जिसे परंपरा और linters लागू करते हैं, भाषा खुद नहीं।

Note: अपने code के तार्किक हिस्सों को दृश्य रूप से अलग करने के लिए एकसमान spacing और खाली lines का उपयोग कीजिए, इसकी कोई performance लागत नहीं और यह पढ़ने वाले हर किसी की मदद करता है।
Warning: While extra whitespace is safe, removing whitespace that separates two distinct words or keywords, like writing constx instead of 'const x', breaks the code entirely.

उदाहरण: Whitespace in JavaScript

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

Line की लंबाई और पठनीयता

एक अकेले JavaScript statement की लंबाई पर कोई सख्त सीमा नहीं है, लेकिन बहुत लंबी lines पढ़ने और review करने में कठिन होती हैं, इसलिए जटिल statements को कई lines में तोड़ना एक आम सर्वोत्तम प्रथा है।

Note: जब statement लंबा हो जाए, तो किसी operator या comma के बाद, एकसमान indentation के साथ, उसे तोड़ने से वह उसके व्यवहार को बदले बिना पठनीय रहता है।
Warning: When manually breaking a statement across lines, make sure the break happens somewhere JavaScript won't insert an unwanted automatic semicolon.

उदाहरण: Line Length and Readability

javascript
const total =
  10 +
  20 +
  30;
// Print `total` to the console
// Print `total` to the console
console.log(total);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.