Uncategorized

Lecture 1 – JavaScript Syntax

Developers use JavaScript statements, placed within the tags to implement JavaScript. The good news is that you can place the script tags anywhere you want in your HTML document. However, it is recommend placing them in the head<head> tags or right at the bottom of the body<body> tags.

It is actually the tag which informs the browser that everything within these tags is a script and should be interpreted as such. The following is a simple syntax of JavaScript.




    
        JavaScript Code
    
</head>
<body>
    
</body>
</html>

The script tag has two important attributes.

  • The Type Attribute: Experts recommend adding type attribute in the script tag. Its value should be “text/javascript”.
  • The Language Attribute: This attribute specifies the programming language you are working with. In our case, its value would be “javascript”.

Your First JavaScript Code

Now it is time to write your first JavaScript Code. In this example, we will print “Hello World” on screen. All we have to do is to call a function document.write. As you can imagine, this function writes or types a string into your HTML document.

<!DOCTYPE html>
<html lang="en">
<head>
    
        document.write('Helo World!');
    
</head>
<body>
    
</body>
</html>

The browser will produce the following result.

When to Use Semicolons

As you can see, we have added a semicolon at the end of the JavaScript code. If each of your JavaScript statement starts on a new line, there is no need to add a semicolon. However, you will have to use them if you write all the statements in a single line as shown in the following example.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>JavaScript Syntax</title>
    
        varSemiColon1 = 10, varSemicolon2 = 20;
    
</head>
<body>
    
</body>
</html>

Case Sensitivity

JavaScript is a case sensitive programming language. It means all the identifiers including functions, keywords, and variables, etc. should have consistent capitalization of letters.

For example, machine and MACHINE are two different identifiers, conveying different messages to the browser. Therefore, you should take necessary care while writing function and variable names in JavaScript.

In the next lecture, we will learn how to place JavaScript in HTML5 document.