JavaScript – Syntax
Hello friends, In this lecture we will discuss
syntax of JavaScript.
JavaScript using statements <script>...
</script> HTML tags in a web page. JavaScript code placed between these
tags.
JavaScript tags can place anywhere in web page, but
it is recommended that should keep in <head> tags.
Example of JavaScript Tags:
Two important attributes of script tag:
- Language: This attribute specifies which scripting language are using. Its value will be JavaScript. But HTML and XHTML have phased out the use of this attribute.
- Type: This attribute is recommended to indicate the scripting language in use and its value set to "text/javascript".
So here is JavaScript syntax:
First JavaScript Code
This is a sample example to print out "Hello
World". It is added the optional HTML comment which is surrounds to
JavaScript code. It is save the code from a browser that does not support
JavaScript.
JavaScript comment ends with the “//-->”. In this
program document.write function used for print.
Example:
Output:
Whitespace and Line Breaks
JavaScript language is ignores tabs, spaces, and newlines.
That spaces, tabs, and newlines freely in program. It is free to format and indent
program.
Semicolons are Optional
JavaScript simple statement are followed by a
semicolon character, like C, C++, and Java. JavaScript allows to omit this semicolon if statements are
placed on a separate line.
Example:
When formatted in a single line
Case Sensitivity
JavaScript is
the case-sensitive language. which means the keywords, variables, function
names, and any other identifiers typed with a consistent capitalization of
letters.
Example: Time and TIME are convey different
meanings.
Comments in JavaScript
JavaScript supports C and C++-style comments.
- Any text between a // is treated as a comment until the end of the line and it is ignored by JavaScript.
- Any text between the/* and */ is a comment. It may span multiple lines.
- HTML opening comment <!—also recognizes JavaScript. It is a single line comment like //.
- HTML closing comment --> is not recognized by JavaScript, but it written as //-->.