JavaScript – Syntax

JavaScript can be actualized utilizing JavaScript articulations that are set inside

the <script>… </script> HTML labels in a site page.

You can put the labels, containing your JavaScript, anyplace inside your page, yet it is ordinarily suggested that

you should keep it inside the <head> labels.

The <script> label cautions the program to begin deciphering all the content between these labels as a content.

A straightforward sentence structure of your JavaScript will show up as pursues.

<script ...>
   JavaScript code
</script>

The content label takes two significant traits −

Language − This property indicates what scripting language you are utilizing. Ordinarily,

its worth will be javascript. Albeit ongoing variants of HTML (and XHTML, its successor) have eliminated the utilization of this quality.

Type − This property is what is presently prescribed to demonstrate the scripting

language being used and it’s worth ought to be set to “content/javascript”.

So your JavaScript portion will look like −

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

Your First JavaScript Code

Give us a chance to take an example guide to print out “Hi World”. We included a discretionary HTML

remark that encompasses our JavaScript code. This is to spare our code from a program that doesn’t bolster JavaScript.

The remark closes with a “// – >”. Here “//” means a remark in JavaScript,

so we add that to keep a program from perusing the finish of the HTML remark as a

bit of JavaScript code. Next, we call a function document.write which composes a string into

our HTML record.

This function can be utilized to compose the content, HTML, or both. Investigate the accompanying code.

<html>
   <body>   
      <script language = "javascript" type = "text/javascript">
         <!--
            document.write("Hello World!")
         //-->
      </script>      
   </body>
</html>

This code will create the accompanying outcome −

Hello World!

Whitespace and Line Breaks

JavaScript disregards spaces, tabs, and newlines that show up in JavaScript programs.

You can utilize spaces, tabs, and newlines uninhibitedly in your program and you are allowed to

the arrangement and indent your projects in a slick and reliable manner that makes the code simple

to peruse and get it.

Semicolons are Optional

Straightforward explanations in JavaScript are by and large pursued by a semicolon character, similarly as they are in C, C++, and Java. JavaScript, be that as it may, enables you to preclude this semicolon if every one of your announcements are set on a different line. For instance, the accompanying code could be composed without semicolons.

<script language = "javascript" type = "text/javascript">
   <!--
      var1 = 10
      var2 = 20
   //-->
</script>

In any case, when arranged in a solitary line as pursues, you should utilize semicolons −

<script language = "javascript" type = "text/javascript">
   <!--
      var1 = 10; var2 = 20;
   //-->
</script>

Note − It is a decent programming practice to utilize semicolons.

Case Sensitivity

JavaScript is a case-delicate language. This implies the language catchphrases,

factors, function names, and some other identifiers should consistently be composed with a predictable

capitalization of letters.

So the identifiers Time and TIME will pass on various implications in JavaScript.

NOTE − Care ought to be taken while composing variable and function names in JavaScript.

Remarks in JavaScript

JavaScript bolsters both C-style and C++-style remarks, Thus −

  • Any content between a/and the finish of a line is treated as a remark and is overlooked by JavaScript.
  • Any content between the characters/* and */is treated as a remark. This may traverse different lines.
  • JavaScript likewise perceives the HTML remark opening grouping <!- – . JavaScript regards this as a solitary line remark, similarly as it does the/remark.
  • The HTML remark shutting grouping – > isn’t perceived by JavaScript so it ought to be composed as/ – >.

Example

The accompanying Example tells the best way to utilize remarks in JavaScript.

<script language = "javascript" type = "text/javascript">
   <!--
      // This is a comment. It is similar to comments in C++
   
      /*
      * This is a multi-line comment in JavaScript
      * It is very similar to comments in C Programming
      */
   //-->
</script>

Leave a Comment

error: Alert: Content is protected!!