Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Syntax of JavaScript

Explore Syntax of JavaScript with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers syntax of JavaScript with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

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 rangesIn 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

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.