JavaScript Syntax - Java Script

What is JavaScript Syntax?

To implement JavaScript, JavaScript statements are to be placed within the <script>... </script> in HTML tags in a web page.

The <script> tags containing your JavaScript can be placed anywhere within you web page, but the reommended method is to keep <script tags> within the <head> tags.

The browser program will be changed by the usage of <script> tag to start interpreting all the text between these tags as a script. A simple example of your JavaScript syntax is as follows.

The script tag has two important attributes −

  • Language − This attribute specifies about the scripting language that you are using. Typically, it will be javascript. The recent versions of HTML (and XHTML, its successor) have discontinued the use of this attribute.
  • Type − This attribute indicates what scripting language you are using and its value should be set to "text/javascript".

So your JavaScript segment looks as follows −

Your First JavaScript Script

Let us consider a sample example to print "Hello World". To save our code from a browser that does not support JavaScript, we need to add HTML comment that surrounds our JavaScript code. The comment ends with a "//-->". Here "//" indicates a comment in JavaScript, so we need to add that, to prevent a browser from reading the end of the HTML comment as a piece of JavaScript code. Next, we need to call a function document.write which writes a string into our HTML document.

This function can be used to write text, HTML, or both. Observe the following code.

The above code will display "Hello World! " as output.

What are Whitespace and Line Breaks?

JavaScript ignores spaces, tabs, and newlines that appear in your JavaScript programs. You can use spaces, tabs, and newlines with out any restrictions in your program and you will be able to format and indent your programs 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 a semicolon, like they are in C, C++, and Java. However, JavaScript allows you to omit this semicolon if each of your statements are placed on a separate line. you can refer the following the code which is written without semicolons.

But when written in a single line , you must use semicolons −

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

Is Java Case Sensitive?

JavaScript is a case-sensitive language. This means that the language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

So the identifiers Time and TIME does not have same meanings in JavaScript.

NOTE −Variables and function names in JavaScript should be written carefully.

How to use Comments in JavaScript?

JavaScript supports both the comment styles of C and C++. Thus

  • Any text between a // and the end of a 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 <!-- which is a opening sequence. JavaScript treats this as a single-line comment, just as it does the // comment, but closing sequence --> should be written as //--> as it is not recognized by JavaScript .


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Java Script Topics