Javascript Syntax By albro

Have you ever come across the word Syntax? This word is a very important word; Both in terms of the meaning of the word itself and in terms of its practical meaning. According to the Oxford dictionary, the general meaning of this word is as follows:
the rules that state how words and phrases must be used in a computer language
(w3schools) JavaScript syntax is the set of rules, how JavaScript programs are constructed.
In other words, in human languages we have "grammar" and in programming languages we have syntax!
Values in JavaScript
JavaScript defines two types of values:
- constant values (literals)
- variable values
We will discuss both types of these values.
Constant values in JavaScript
The most important rules for writing constant values are the following:
Numbers can be written with or without decimals. Example:
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Numbers< /h2>
< p>Number can be written with or without decimals.< /p>
< p id="demo">< /p>
< script>
document.getElementById("demo").innerHTML = 10.50;
< /script>
< /body>
< /html>

Strings are treated as text and can be enclosed between double quotations or single quotations. Example:
"Hive Blockcahin JS Bot with @albro"
'Hive Blockcahin JS Bot with @albro'
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Strings< /h2>
< p>String can be enclosed between double quotations or single quotations.< /p>
< p id="double">< /p>
< p id="single">< /p>
< script>
document.getElementById("double").innerHTML = "Double Quotation: Hive Blockcahin JS Bot with @albro";
document.getElementById("single").innerHTML = 'Single Quotation: Hive Blockcahin JS Bot with @albro';
< /script>
< /body>
< /html>

Variable values in JavaScript
Variable values are stored in variables. Variables are used in programming languages to store data. JavaScript uses the word var to define variables and the equal sign (=) to assign values to variables. In the following example, X is defined as a variable and then the value 6 is assigned to it:
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Variables< /h2>
< p>In this example, x is defined as a variable.
Then, x is assigned the value of 6:< /p>
< p id="demo">< /p>
< script> var x;
x = 6;
document.getElementById("demo").innerHTML = x;
< /script>
< /body>
< /html>

Operators in JavaScript
JavaScript uses arithmetic operators to calculate values(+ - * /). Example:
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Operators< /h2>
< p>JavaScript uses arithmetic operators to compute values (just like algebra).< /p>
< p id="demo">< /p>
< script>
document.getElementById("demo").innerHTML = (5 + 6) * 10;
< /script>
< /body>
< /html>
The output of this code will be 110
The assignment operator (=) is used to assign values to variables. Example:
< !DOCTYPE html>
< html>
< body>
< h2>Assigning JavaScript Values< /h2>
< p>In JavaScript the = operator is used to assign values to variables.< /p>
< p id="demo">< /p>
< script> var x, y;
x = 5;
y = 6;
document.getElementById("demo").innerHTML = x + y;
< /script>
< /body>
< /html>

Expressions in JavaScript
An expression is a combination of values, variables and operators that leads to a value. Calculating this expression is called evaluation. Simple example:
5 * 10
These expressions can also include variables:
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Expressions< /h2>
< p>Expressions compute to values.< /p>
< p id="demo">< /p>
< script> var x;
x = 5;
document.getElementById("demo").innerHTML = x * 10;
< /script>
< /body>
< /html>

These expressions can also contain string values:
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Expressions< /h2>
< p>Expressions compute to values.< /p>
< p id="demo">< /p>
< script>
document.getElementById("demo").innerHTML = "Hive" + " " + "Blockchain";
< /script>
< /body>
< /html>
The output of this code will be "Hive Blockchain".
Comments in JavaScript
Not all statements can be executed in JavaScript. Comments are from this category of statements. You can write comments in two ways:
- Single line comment: The text of the comment is placed after
//. - Multi-line comment: Comment text is placed between
/*and*/.
Note: The difference between a single-line comment and a multi-line comment is that if you use a single-line comment, after pressing the enter key or exiting that line in any way, what you write will not be considered as a comment. Example:
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript Comments are NOT Executed< /h2>
< p id="demo">< /p>
< script> var x;
x = 5;
// x = 6; Not Execute
/* This code
Not
Execute */
document.getElementById("demo").innerHTML = x;
< /script>
< /body>
< /html>
Identifiers in JavaScript
Identifiers are used in JavaScript to name variables, keywords, functions and labels. Naming rules are the same in most programming languages. In JavaScript, the first letter of Identifiers (the same variable name, or function name, etc.) must be a letter, or an underscore (_) or a dollar sign ($). The letters after the first letter can be letters, numbers, underscores or dollar signs. Therefore, numbers are not acceptable as first letters.
Important point: Variables in JavaScript are case sensitive; That is, they are sensitive to small and large English letters. For example, a variable called Platform is not the same as a variable called platform!
< !DOCTYPE html>
< html>
< body>
< h2>JavaScript is Case Sensitive< /h2>
< p>Try change lastName to lastname.< /p>
< p id="demo">< /p>
< script> var lastname, lastName;
Platform = "Platform is Uppercase";
platform = "platform is Lowercase";
document.getElementById("demo").innerHTML = Platform;
< /script>
< /body>
< /html>
The output of this code will be the string "Platform is Uppercase".
So be sure to pay attention: based on what was said, you should know that JavaScript does not recognize VAR or Var as the var keyword (to define a variable)!










Comments