JS Statements
In this page:
statement1;
statement2;
let variable = value;
Statement क्या है
Statement एक अकेला निर्देश है जिसे JavaScript engine execute करता है, जैसे variable घोषित करना, function बुलाना या कोई नया value assign करना। JavaScript program असल में बस statements का एक क्रम है जो एक के बाद एक, क्रम से चलता है।
उदाहरण: What Is a Statement
// 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 को ज़्यादा पूर्वानुमेय और पढ़ने में आसान बनाता है।
उदाहरण: Semicolons
// 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 हों।
उदाहरण: Code Blocks
// 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 लागू करते हैं, भाषा खुद नहीं।
उदाहरण: Whitespace in JavaScript
let x = 5;
console.log(x);
Line की लंबाई और पठनीयता
एक अकेले JavaScript statement की लंबाई पर कोई सख्त सीमा नहीं है, लेकिन बहुत लंबी lines पढ़ने और review करने में कठिन होती हैं, इसलिए जटिल statements को कई lines में तोड़ना एक आम सर्वोत्तम प्रथा है।
उदाहरण: Line Length and Readability
const total =
10 +
20 +
30;
// Print `total` to the console
// Print `total` to the console
console.log(total);
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: