ECMAScript ग्रामेटिक

जावा, C और पर्ल जैसी भाषाओं के विकासकर्ता एसीमेस्क्रिप्ट के व्याकरण को आसानी से पकड़ने में मदद मिलेगी, क्योंकि यह इन भाषाओं के व्याकरण को अपनाता है।

जावा और ECMAScript कुछ महत्वपूर्ण व्याकरण विशेषताएँ समान हैं, कुछ पूरी तरह से अलग हैं।

case-sensitive

like Java, variables, function names, operators, and everything else are case-sensitive.

for example:

variable test is different from variable TEST.

variables are weakly typed

unlike Java and C, variables in ECMAScript do not have a specific type, only the var operator is used to define a variable, which can be initialized to any value.

therefore, the type of data stored in the variable can be changed at any time (try to avoid doing this as much as possible).

example

var color = "red";
var num = 25;
var visible = true;

a semicolon at the end of each line is optional

Java, C, and Perl require that each line of code ends with a semicolon (;) to be syntactically correct.

ECMAScript allows developers to decide themselves whether to end a line of code with a semicolon. If there is no semicolon, ECMAScript considers the end of a line break as the end of the statement (similar to Visual Basic and VBScript), provided that this does not disrupt the semantics of the code.

the best coding habit is to always add semicolons, because without semicolons, some browsers cannot run correctly, but according to the ECMAScript standard, the following two lines of code are both correct:

var test1 = "red"
var test2 = "blue";

comments are the same as in Java, C, and PHP languages

ECMAScript has borrowed these languages' comment syntax.

there are two types of comments:

  • single-line comments are started with double slashes (//)
  • multi-line comments are started with a single slash and an asterisk (/*), and end with an asterisk and a single slash (*/)
//this is a single-line comment
/*this is a multi-
line comment*/

बांधे बांधे कोड ब्लॉक के रूप में प्रदर्शित किया जाता है

जावा से ली गई एक और अवधारणा है कोड ब्लॉक。

कोड ब्लॉक एक श्रृंखला के बारे में है, जो वर्गीकृत किया जाता है कि इसे जोड़े जाने चाहिए, इसे बाएं बांधे ( { ) और दाएं बांधे ( } ) के बीच बंद किया जाता है。

उदाहरण:

if (test1 == "red") {
    test1 = "blue";
    alert(test1);
}