Javascript standard basic rules
The codes in a Javascript file are formed by the sequential assembly of statements, that is, executable codes. JavaScript expressions are commands given to the browser.
The purpose of these commands is to tell the browser what to do.
SAMPLE:
<script>
document.getElementById("test").innerHTML = "Hello world";
</script>
The above expression prints Hello world where id="test" is from HTML tags.
JavaScript code is a string of JavaScript expressions.
Statements are run by the browser in the order in which they are written.
SAMPLE:
<script>
document.getElementById("demo").innerHTML="Hello World!";
document.getElementById("test").innerHTML="Hello javascript";
</script>
In the above example id="demo" is "hello world" to the HTML element. Writes "hello Javascript" to the HTML element with id="test". In javascript software, the codes work in order from the upper expression to the following expression.
In JavaScript, statements are terminated with a semicolon or a return statement.
For this, semicolons are placed between the expressions on the same line to separate the expressions from each other. Each statement is an executable code sample. If a statement is expressed on a single line, a semicolon should be placed at the end of the line.
Using semicolons allows us to write many statements on one line.
You may have seen examples without commas. Ending statements with a semicolon is optional in JavaScript.
It is normal to add a semicolon at the end of each executable statement. Most Webmasters consider it a good practice for good programming.
SAMPLE:
<script>
var k = 0 ; i++ // <- Semicolon mandatory two statements on the same line
var i = 0 // <- semicolon optional
i ++ // <- semicolon optional
</script>
JAVASCRIPT CODE BLOCKS
Javascript expressions come together to form code blocks.
Code blocks begin with a left curly "{" parenthesis and end with a right curly "}" parenthesis. All statements in the group separated by the block are executed by the browser at the same time, producing the result. The best example of code blocks are Javascript functions.
SAMPLE:
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
document.getElementById("test").innerHTML = "Hello javaScript";
}
JAVASCRIPT WHITE SPACE
Javascript ignores extra spaces.
You can use spaces to make code more readable.
The following two lines of Code are equivalent:
SAMPLE:
var book = "JavaScript";
var book="JavaScript";
JAVASCRIPT CASE SENSITIVITY
Pay attention to the letters you type when writing JavaScript expressions. JavaScript is case sensitive. The "getElementById" function is not the same as the "getElementbyID" function.
JAVASCRIPT CODE LINE SPLIT
Our code can span a very long line. In such cases, we cannot see the whole code, making it difficult for us to work. You can split a line of code along with a backslash. As you can see in the example below, the row will be displayed normally.
SAMPLE:
<script>
document.write("Hello \
JavaScript!");
</script>
You cannot split a line of code as follows:
<script>
document.write \
("Hello JavaScript!");
</script>
JAVASCRIPT CHARACTER SET (Character Set)
The character set used in the JavaScript language is the Unicode character set. This character set contains all the characters, signs and symbols used in the world.
JAVASCRIPT LINE LENGTH AND LINE ENDINGS
Webmasters do not want to write code statements longer than 80 characters for best readability of JavaScript code statements.
If the JavaScript expression does not fit on a line, it is best to end the line after an operator.
SAMPLE:
<script>
document.getElementById("demo").innerHTML =
"Hello JavaScript!";
</script>
JAVASCRIPT FUNCTIONS
JavaScript statements are written inside the function and the function is called multiple times to make the expression work.
SAMPLE:
<script>
function find sum(x,y) {
return x + y; //sum of x and y values returns
}
</script>