JavaScript Syntax

JavaScript can be implemented using embedded JavaScript statements

You can place the <script> tags, containing your JavaScript, anywhere on your web page, but it is generally recommended to place them inside  <head> tags.

The <script>  tags cause the browser program to start converting all text between these tags as text. A simple syntax of your JavaScript will appear like the following.

The script tag takes two important features –

Language – This property specifies the text language you are using. Of course the value would be javascript. Although the new version of HTML (and XHTML, its successor) has stopped using this attribute.

Type – This attribute is currently supported to indicate the script language used and its value should be set to “text/javascript”.

Your first JavaScript code

Let’s take an example to print “Hello World”. We’ve added an optional HTML comment wrapped around our JavaScript code. This includes protecting our code in browsers that do not support JavaScript. The word ends with “//–>”. Here, “//” means a word in JavaScript, so we add it so that browsers don’t read the end of HTML as a piece of JavaScript code. Next, we call the document. write function which writes a string to our HTML document.

White space and line breaks

JavaScript ignores spaces, tabs and newlines appearing in JavaScript programs. You’re free to use spaces, tabs, and newlines in your programs, and you’re free to structure and code your programs in a neat, flexible way that makes code easier to read and understand.

Semicolon is optional

Simple expressions in JavaScript are often followed by a unit, just as they are in C, C++, and Java. JavaScript, however, allows you to omit the semicolon if each of your words is placed on a separate line. For example, this code can be written without a semicolon.

case-sensitive

JavaScript is a case-sensitive language. This means that language keywords, variables, function names and any identifiers will always use a consistent font. Therefore, time and time identifiers will have different meanings in JavaScript.

NOTE – Care should be taken when writing variable names and function names in JavaScript. Word and JavaScript
JavaScript supports both C-style and C++ style, so –

Comments in JavaScript

  • Any text between // and the end of a line is treated as a comment and is ignored by JavaScript.
  • Any text between the characters /* and */ is considered a comment. This can span multiple lines.
  • JavaScript also recognizes the opening is not accepted for JavaScript, so it must be written as //–>.
  • JavaScript also recognizes <!–. the initialization of HTML words. JavaScript treats this as a single line expression, as it says.
  • JavaScript does not understand HTML comment closures–> , so it should be written as follows  //–>.