JavaScript A to Z Tutorial : Syntax & Programming Style

javascript tutorial syntax

JavaScript Statements

A JavaScript statement consists of variables, keywords, comments, etc. Which is executed Line-By-Line.  A JavaScript program is a collection of many statements.

Example

 document.write("Hey Geeks!");

The given JavaScript statement tells the browser to output “Hello Geeks!” will be displayed.

JavaScript Syntax

We embed the JavaScript code inside the <head></head> tag in the HTML code. All the code is inside the <script>………</script> tag. When the browser renders an HTML document, if it finds a <script> tag, it starts running the JavaScript statements.

    <script>
        .......
        
        JavaScript Statements//
        
        .......
    </script>

Programming Style

Javascript is a very easy-to-use programming language, it is very fun to code in it. You can run its code in three ways by adding it to your HTML document. 

(1) in <head>

<head> 

<script language="javascript" type="text/javascript">
         
       document.write("Hey Geeks!");

        
    </script>
</head>

(2) in <body>

<body> 

<script language="javascript" type="text/javascript">
         
       document.write("Hey Geeks!");

        
    </script>
</body>

(3) By External JS file

Large JavaScript projects contain a large amount of source code, which can be a headache to manage. That’s why we save the JavaScript statements in an external with extension .js file and link them in the <head> section of the HTML document.

alert.js

function AlertMsg() {



    alert("Hello Geeks!")

}

You will write all the JS code in the external file with the .js extension, you do not need to put <script> tag in it.

myDoc.html

<!DOCTYPE html>
<html lang="en">

<head>
    <script type="text/javascript" src="alert.js"></script>
</head>

<body>

    <button onclick="AlertMsg()">Click Here</button>

</body>

</html>

Semicolons in JS

Semicolons (;) are used to separate each executable statement in JavaScript. The browser terminates the statement with a semicolon and runs the next statement line by line.

document.write("Hello Geeks");

var a=10;

var b=20;

White Spaces

You can use space in JavaScript so that your code looks good, the browser ignores these white spaces.

Statement-1 without white spaces : alert("Hello Geeks!");

Statement-2 with white spaces: alert ( "Hello Geeks!" ) ;

The output of both the given statements is the same, so you can write these statements in any way, both are equal.

JavaScript Comments

JavaScript comments are commonly used for documentation of source code. These are meant to tell the purpose of writing statements about the code to other programmers. With this, another person can easily understand and make changes in any particular piece of code.

Consider you have 1000’s of off-line code, now you have to edit or debug it. But the person who created this code did not give you any information about the code. In such a situation, it can be a headache for you to fix the code. To overcome this problem, the concept of comments is used. 

There are 2 types of comments used in JavaScript.

1. Single Line Comments

It provides the information of a code in a single line. We use → // to comment out a piece of code.

Syntax:

//This is a single line comment.

2. Multiline Comments

As the name suggests, multiline comments are used to provide information about the code in multiple lines.

Syntax:

document.write("Hello Geeks!")  

/*
This is Multiline comment
Here we are learnin JavaScript
This is a simple JS program.
*/
Neeraj Yadav
Howdy Friends! I am Neeraj Yadav a professional Web and App developer. I am trying to share my knowledge through this website. Here you will get amazing coding projects, free courses, programming articles.