Javascript Syntax and Rules

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.

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.

Semi-Colon

; the semicolon is used to indicate the end of a statement.

For Example

But, they are optional if you use a single line for each statement

For Example, this is also valid

But, they are required if you have multiple statements in a line

For Example

They are also required in some cases where interpreter fails to correctly identify the end of statement.

For Example

The interpreter interprets this as and throws the error 2 is not a function

Hence you need to add an semi colon

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

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.

Multi-line comments (/* */) − These comments may span multiple lines.

Example

Nested comments are not supported

References

1 thought on “Javascript Syntax and Rules”

  1. 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.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top