Posted By:
26 Aug, 2014 10:41 am PDT

JavaScript Code

Please give an example of JavaScript code which also explains the concept of comments and statements.

Attachment:None


First JavaScript Code

 

<html>

<body>

<script language="javascript" type="text/javascript">

<!--

   document.write("Hello World!")

//-->

</script>

</body>

</html>

 
WhiteSpaces and LineBreaks

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs.

Because you can use spaces, tabs, and newlines freely in your program so you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.

Semicolons

Simple statements in JavaScript are generally followed by a 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 a separate line. For example, the following code could be written without semicolons

 

<script language="javascript" type="text/javascript">

<!--

  var1 = 10

  var2 = 20

//-->

</script>

 

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

<script language="javascript" type="text/javascript">

<!--

  var1 = 10; var2 = 20;

//-->

</script>

 

Case Sensitivity

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

So identifiers Time, TIme and TIME will have different meanings in JavaScript.

Comments in JavaScript

JavaScript supports both C-style and C++-style comments, 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 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 //-->.

Example:

 

<script language="javascript" type="text/javascript">

<!--

 // This is a comment. It is similar to comments in C++

 /*

 * This is a multiline comment in JavaScript

 * It is very similar to comments in C Programming

 */

//-->

</script>

Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud