Comments are actually lines in the source code that are not executed. These lines are used for different purposes, which I will mention the most important uses:
For this reason, they comment in group work so that if another developer takes over part of the work, he/she doesn't spend her time reading your code.
You can see an example of explanatory comments in the following example:
// Change heading:
document.getElementById("myH").innerHTML = "JavaScript Comments";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";
As you can see, JavaScript codes are explained to make them clearer. In a small project like this, they may not be very important, but when your source code has 900 lines, you will appreciate them!
Variable naming rules in JavaScript:
$).Reserved words in programming languages are special words that have been reserved by their developers for special purposes. For example, the word function is a reserved word that is used to define functions. You cannot name your variables function. Of course, you can add something to it; For example, myFucntion is fully accepted.
The following table includes some of the most important reserved words
Note: The assignment operator means assignment, not equal!
In JavaScript, the assignment operator, which is indicated by the equal sign (=), is used to declare the assignment of a value to a variable, not that they are equal!
For example, the expression x = x + 5 does not make sense in algebra and is wrong (in this science, "=" is the equal sign), but in JavaScript it makes perfect sense (in JavaScript, "=" is the assignment sign); The meaning of this expression in JavaScript is: calculate the value of x + 5 and assign it to x! In other words, the value of x increases by 5 units).
I've written a code for you to understand this issue:
JavaScript Variables
var x = 2;
document.getElementById("demo1").innerHTML = "Firt value: " + x;
x = 3;
document.getElementById("demo2").innerHTML = "Second value: " + x;
x = x + 5;
document.getElementById("demo3").innerHTML = "Third value: " + x;
The values were 2, 3 and 8 respectively! You can see that x = x + 5 means that variable x becomes itself plus 5! And it is absolutely correct.
If you are curious, I should say that the equals operator is represented by the == sign. Therefore, = becomes an assignment sign and == becomes an equality sign! We will discuss this in future episodes.
In programming languages, text letters are called string (because a string of letters are placed together and form a word or sentence).
Strings are enclosed in double quotation marks (" ") or single quotation marks (' '). According to this saying, if we put the numbers inside " " or " " symbols, they will be treated like strings! Pay close attention to the following example:
JavaScript Variables
Strings are written with quotes.
Numbers are written without quotes.
var number = 10;
var secondNumber = 5;
var stringNumber = "10";
var myString1 = "@";
var myString2 = "albro";
document.getElementById("demo1").innerHTML = number + secondNumber;
document.getElementById("demo2").innerHTML = number + stringNumber;
document.getElementById("demo3").innerHTML = myString1 + myString2;
You can see that the sum of the numbers 5 and 10 is equal to 15, but if we define the same numbers as a string, it means that they are no longer numbers but strings. On the other hand, the sum between the fields is such that they are put together.
Therefore, the sum between "10" and "10" becomes "1010"! If you use " " or ' ' signs during a string of operations on one of the numbers, the rest of the numbers are also considered as strings.
Note: You don't need to give values to variables when defining them in JavaScript. You can postpone the value for later. Example:
var accountName;
The above code defines a variable called accountName and currently has no value. Later we can give it a value:
accountName = "albro";
Pay attention that I don't use the word var again when setting the value, because we do not intend to define it.
Practical tip: You can define several variables in one statement.
For this, you must use commas between the variable names. Example:
JavaScript Variables
You can declare many variables in one statement.
var accountName = "@albro", platformName = "Hive", address = "Blockchain";
document.getElementById("demo").innerHTML = accountName + " in " + platformName + " at " + address;
The output of this code will be "@albro in Hive at Blockchain".
Note: If you redefine a variable that you have already defined (of course without setting it), its initial value will not change. Example:
var accountName = "@albro";
var accountName;
The accountName value does not change.