In this article let us look at Syntax and Basic Rules, which we need to follow while writing JavaScript code. You can refer to the tutorial Hello World example using JavaScript to learn how to create and run a Javascript Program.
Table of Contents
Case-sensitivity
JavaScript is case-sensitive. This means that foo
is not the same as Foo
. Test
is not same as test
.
Statements & Expressions
The JavaScript has two syntactic distinctions. one is statement & the other one is expression.
Statements
A JavaScript statement performs some action but does not produce any value. Optionally they end with a semicolon.
A Typical Javascript program consists of several such sequences of statements and they control the flow of the program. The JavaScript interpreter executes these statements one by one in the order they are specified in the Program.
For Loops and if statements are examples of statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 | //statement that create a variable var message; //statement that assigns “Hello World” to message variable message=”hello world”; //statmenent that prints out console log console.log(message); // |
Semi-Colon
;
the semicolon is used to indicate the end of a statement.
For Example
1 2 3 4 5 | var message; message=”hello world”; console.log(message); |
But, they are optional if you use a single line for each statement
For Example, this is also valid
1 2 3 4 5 | var message message=”hello world” console.log(message) |
But, they are required if you have multiple statements in a line
For Example
1 2 3 | var message ; message=”hello world” ; console.log(message) ; |
They are also required in some cases where interpreter fails to correctly identify the end of statement.
For Example
1 2 3 4 5 | var c = 1 + 2 (1 + 2).toString() |
The interpreter interprets this as and throws the error 2 is not a function
1 2 3 | var c = 1 + 2(1 + 2).toString() |
Hence you need to add an semi colon
1 2 3 4 | var c = 1 + 2; (1 + 2).toString(); |
Expressions
An expression is any valid unit of code that resolves to a value. They can appear anywhere in the code. They can be part of the function arguments or right side of an assignment, etc. The Expressions are often part of a statement.
When interpreter sees an expression it retrieves its value and replaces expression with new value. The following codes are examples of the expressions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //Simple Expression that produces numeric value 5 5; // Airthmetic Expression that produces numeric value 15 5+15; //String expression that evaluates to 'hello world' string 'hello' + 'world'; // //Argument in a function call are expressions addNum(5,15) |
Whitespace and Line Breaks
You can use spaces, tabs, and newlines anywhere in the Javascript Program. The Javascript interpreter ignores them. Use tabs & spaces to neatly format or indent your code. It makes the code easy to read & understand.
Comments
The JavaScript allows us to add single line comments or Multi line comments
Single-line comments ( // ) − Any text between a // and the end of a line is treated as a comment.
1 2 3 | //this is a single-line comment |
Multi-line comments (/* */) − These comments may span multiple lines.
Example
1 2 3 4 5 6 7 8 | /* This is a Multi-line comment */ |
Nested comments are not supported
1 2 3 4 5 | /* /*This Comments not allowed*/ */ |
wow, superb. it is very nice tutorial.
very good information for preparing for the interviews
Hello dear, I always read your all post because it gives us lots of information which is very helpful for all of us. Thank you so much for enhancing our knowledge.