Friday, December 7, 2018

JavaScript Syntax

Hello guys morning to you all and welcome to this tutorial post on JavaScript. Like you all know i started my tutorial on JavaScript in my previous tutorial post and we looked at a brief JavaScript Overview. Now in this tutorial post am gonna be discussing JavaScript Syntax.

A JavaScript consists of JavaScript statements that are placed within the <Script>.....</Script> HTML tag in the web page.

You can place the <Script> tag containing the JavaScript code anywhere within your web page but it is preferred way to keep it within the <head> tags.

The <Script> tag alert the browser to start interpreting all the texts between these tags as a script. So simple syntax of your JavaScript will be as follows.

JavaScript Code

You can also read: Css Positioning

The Script tag takes two important attributes:
  • language: This attribute specifies what scripting language you are using. Typically it's value will be JavaScript. Although recent versions of HTML(and XHTML, its successor) have phased out the use of this attribute.
  • type: This attribute is what is now recommended to indicate the scripting language in use and its value should be set to text/javascript.
So your JavaScript segment will look like:

<Script   language="javascript"  type="text/javascript">
JavaScript Code

Your First JavaScript Code:
Let us write a simple JavaScript code that will print "Hello World".

<Script   language="javascript"  type="text/javascript">
document.write("Hello World!")

We added an optional HTML comment that surrounds our code. This is to save our code from a browser that does not support JavaScript. The comment ends with a "//-->" and here "//" signifies a comment in JavaScript, so we add that to prevent the browser from reading the end of the HTML comment in as a piece of JavaScript code.
Next we call a function document.write which writes a string into the HTML document. This function can be used to write text, HTML, or both. So above code will produce the result below.

Hello World!

You can also read: Pseudo elements

Whitespace and Line Breaks:
JavaScript ignores spaces, tabs, and newlines that appear in JavaScript program.
Because you can use spaces, tabs, newlines freely in your program so you are free to format and indent your program in a neat and consistent way that makes the code easy to read and understand.

Semicolons are optional:
Simple statements in JavaScript are generally followed by semicolon character, just as they are in C, C++, and Java. JavaScript however allows you to omit this semicolon if your statements are each placed on separate line. For example, the following codes could be written without semicolons

<Script   language="javascript"  type="text/javascript">
var1 = 10
var2 = 20

But when formatted in a single line as follows, the semicolons are required:

<Script   language="javascript"  type="text/javascript">
var1 = 10;   var2 = 20;

Note: It is a good programming practice to use semicolons.

You can also read: Css Visibility

Case Sensitivity:
JavaScript is a case sensitive language. This means that language keywords, variables, function names, and any other identifiers must be typed with a consistent capitalization of letters.
So identifiers Time, TIme, and TIME will have a different meaning in JavaScript.
Note: Care should be taken while writing your variable and function names in JavaScript.

Comments in JavaScript:
JavaScript supports both C-style and C++-style comments, thus:
  • Any text between a // and the end of the line is treated as a comment and is ignored by JavaScript.
  • Any text between the characters /* and */ is treated as a comment. This may span multiple lines.
  • JavaScript also recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single line comment, just as it does the // comment.
  • The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->

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

Alright guys we have come to the end of this tutorial post on JavaScript Syntax. Always feel free to ask your questions in areas you don't understand properly, and i promise to attend to your questions as soon as possible. 

This blog has a chat box that is meant for interaction, so you guys should always feel free to interact there and share ideas, or you can also interact in the comment box below.

Like our facebook page and subscribe with us to get our tutorial posts delivered directly to your emails. Also share this tutorial post on the various social media platforms available.

You can follow us on twitter 
You can follow us on google+ 
You can also follow us on pinterest  


Post a Comment