JavaScript Tutorial & Demo 3: Some more fundamentals & Hello World!

The Script Tag

JavaScript may be used to create web pages by inserting JavaScript statements between the <script>… </script> HTML elements.

Demo Syntax

<script>
   JavaScript code
</script>

You may insert the <script> tags containing your JavaScript wherever on your web page, but it is usually best to keep it within the head> tags.

The <script> element instructs the browser to begin reading all content between these tags as a script. The following is a basic syntax for your JavaScript.

Embed JavaScript code in an HTML page

The script tag requires two critical elements:

Language: This element defines the scripting language that is being used. Its value is often javascript even though subsequent versions of HTML (and its successor, XHTML) have phased out using this feature.

Type: It is an attribute now suggested to identify the scripting language in use, and its value should be set to “text/javascript.”

If you want to preserve your JavaScript code within the HTML document, you should put it all within script> tags (script> and /script>). This allows your browser to distinguish between your JavaScript code and the rest of the code. Because there are alternative client-side scripting languages (for example, VBScript), it is strongly advised that you identify the scripting language you intend to utilize. You must include the type attribute within the script> element and set its value to text/javascript, as seen below:

<script type="text/javascript">

Placing JavaScript code straight inside the <script> element is not encouraged and should only be used for proof of concept or testing.

From top to bottom, the JavaScript code in the <script> element is interpreted. As an demo example:

<!DOCTYPE html>
<html >
<head>
    <title>JavaScript Hello World!</title>
    <script>
        alert('Hello, World!');
    </script>
</head>
<body>
</body>
</html>

Positions for JavaScript code

  • Between the html body tag
  • Between the html head tag
  • In the.js file (external javaScript)

Line Breaks and Whitespace in JavaScript

JavaScript disregards spaces, tabs, and newlines in JavaScript scripts. You may freely utilize spaces, tabs, and newlines in your program, and you can style and indent your programs in a tidy and consistent manner that makes the code easy to read and comprehend.

Semicolons in JavaScript

Simple statements in JavaScript, like those in C, C++, and Java, are usually followed by a semicolon character. JavaScript, on the other hand, permits you to eliminate this semicolon if each of your statements is on a distinct line. Semicolons are an excellent programming technique. The following code, for example, might be written without semicolons.

Demo Example 1: written without semicolons

<script language = "javascript" type = "text/javascript">
   <!--
     x = 1
     y = 2
   //-->
</script>

However, semicolons must be used when formatting in a single line, as seen below.

Demo Example 2: written withsemicolons

<script language = "javascript" type = "text/javascript">
   <!--
      x = 1; y= 2;
   //-->
</script>
Note: JavaScript is a case-sensitive programming language. This implies that all language keywords, variables, function names, and other identifiers must be entered with consistent letter capitalization.

As a result, in JavaScript, the identifiers “int” and “INT” will have distinct meanings.

Comments in JavaScript

JavaScript accepts C-style and C++-style comments, consequently:

  • JavaScript treats any text between a / and the end of a line as a comment and ignores it.
  • Any text between the symbols /* and */ is considered a comment. This might span several lines.
  • JavaScript recognizes the HTML comment beginning sequence!— as well. This is treated as a single-line comment by JavaScript, much like the / comment.
  • Because JavaScript does not recognize the HTML comment closing sequence –>, it should be expressed as /—>.

Demo Example 3: Comments in JavaScript

 <script language = "javascript" type = "text/javascript">
 <!--
      // This is a comment in JavaScript
   
      /*
      * This is a multi-line comment in JavaScript
      * You may find same way in C language 
      */
   //-->
</script>

Hello World! Demo Examples using JavaScript

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

The code will give following result:

Hello World!

If you have noticed earlier we have used:

<script>
        alert('Hello, World!');
 </script>

When you’ll run that program, you will get a pop-up notification saying: Hello World!

Article Category: JavaScript Tutorials

More JavaScript Tutorials

4 thoughts on “JavaScript Tutorial & Demo 3: Some more fundamentals & Hello World!”

  1. Pingback: Lusia Jones

Leave a Comment