Learn JavaScript

JavaScript Syntax

A JavaScript program can be implemented by JavaScript statements, written in a proper syntax. The JavaScript syntax is nothing but a set of rules that defines an structured JavaScript program. A complete structure of a JavaScript program is given as follows:

// Comments
<html>
<body>
<script>
Statement 1;
Statement 2;
Statement 3;
.
.
.
Statement n;
</script>
</body>
</html>

JavaScript Syntax Basics


Case Sensitivity

JavaScript is a case sensitive programming language. The first letter of a constructor must be a capital letter, while the names of a function or a variable must start with a lowercase letter.

Example

// JavaScript program to print the value of a variable
<html>
<body>
<script>
var a=2;
console.log(a); // Output will be 2.
console.log(A); // throws a Reference Error: A is not defined
</script>
</body>
</html>

Whitespace and Semicolons

JavaScript ignores the characters like whitespace, tabs, and newline that usually appears in a JavaScript program, that means you are free to use these characters in your program as per your need. But it is advisable to use them in such a manner, that the program must be easy to read and understand by other programmers too.
Unlike other programming languages such as: C, C++, and Java where the use of semicolon is must at the end of each statement, JavaScript allows you to omit the semi column (because of automatic semicolon insertion ASI), if you are writing your statements on a separate lines. But for a better programming practice it is advisable to use the semicolon.

Example

<script>
a=2
b=3
</script>

// treated as:
// a=2;
// b=3;

Note: The five tokens {open parenthesis “(“, open bracket “[“, slash “/”, plus “+”, and minus “-“} can either begin a statement or can give a complete extension to a statement, potentially leads to an incorrect parsing.

Example 1

<script>
return
a+b;
</script>

// Returns undefined
// treated as:
// return;
//a+b;
// correct statement is:
//return a+b;

Example 2

<script>
a = b + c
(d + e);
</script>

// treated as:
// a = b + c(d + e);

Comments

The characteristic of comments in JavaScript programming language is same as it is in other programming languages like: C, C++, Java, etc.

Example

// treated as sigle line comment

/* treated as the multi line long comment */

Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.