Syntax of JavaScript

JavaScript can be implemented using JavaScript commands that are placed in HTML tags ... in a web page.

JavaScript can be implemented using JavaScript commands that are placed in HTML tags in a web page.

You can place the card

The script tag takes the following two important properties:

Language - This property specifies which scripting language you are using. Its value will be javascript. Although recent versions of HTML (and XHTML) have stopped using this attribute again.

Type - This attribute is what is now recommended to use and its value should be set to "text / javascript".

Here's a snippet of JavaScript:

    language = "javascript" type = "text/javascript" > JavaScript  code  

Your first JavaScript script

Now we make a sample to print "Hello World". We add an arbitrary HTML commet that surrounds JavaScript code. This preserves our code when a browser does not support JavaScript. The comment ends with a "// ->". Here "//" represents a comment in JavaScript, so we add it to prevent a browser from reading the comment as part of JavaScript code. Next, we call a documet.write function that writes a string into our HTML document.

This function can be used to write text, HTML or both. See the following code:

    language = "javascript" type = "text/javascript" >     

It will produce the following result:

  Hello World! 

White space and interrupt line

JavaScript ignores new spaces, tabs, and lines that appear in JavaScript programs. You can use free spaces, tabs, and lines in your program and you are free to format and indent your program and the proper way to create code that is easy to read and understand.

The semicolon is optional

Simple commands in JavaScript are generally followed by a semicolon character, which when they are in C, C ++, and Java. However, JavaScript allows you to ignore this semicolon if each of your commands is placed on a separate line. For example, the following code can be written without using a semicolon:

    language = "javascript" type = "text/javascript" >     

But when formatted in a single line as follows, you must use semicolons:

    language = "javascript" type = "text/javascript" >     

Note - When practicing, it is better to use semicolons.

Differentiate typefaces

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be written correctly.

Therefore, the two identifiers TIME and Time will have different meanings in JavaScript.

Note : You should be especially careful when writing function variables and names in JavaScript.

Comments in JavaScript

JavaScript supports both comment styles of C and C ++, so:

Any text between a // and the end of a line is treated as a comment and ignored by JavaScript.

Any text between the / * and * / characters is considered a comment. It can spread over many lines.

JavaScript also recognizes HTML comments with open ranges in HTML comments are not recognized by JavaScript so it should be written as // ->.

For example

The following example shows how to use comments in JavaScript:

    language = "javascript" type = "text/javascript" >     

Follow tutorialspoint

Previous article: What is JavaScript?

Next lesson: Enable JavaScript in browsers