Basic Programming Tutorials

javascript syntax

Welcome to the JavaScript tutorial, in the last topic we covered where to use JavaScript, and today we will discuss JavaScript syntax.  As we know if you will not implement syntax for any language properly, then the result will throw an error. At SharpLesson, we will learn JavaScript syntax with all possible effort of making it short and simple.

JavaScript Syntax

JavaScript statements have to be placed in <script> tags, like this:

<script>

Your javascript code

<script>

JavaScript syntax has both starting and closing script tag. And we can place JavaScript anywhere in the page, don’t worry, we have covered where to place JavaScript as well.

Once browser will find <script> tag, they will understand that “ The text between these tags has to be treated as script”.


Test your JavaScript

Never forget to test your JavaScript, because of the following things

  • Human error
  • Browser compatibility issues
  • Operating system differences

So good practice says, test your JavaScript for above problems.

JavaScript syntax – Use in Example

Let’s see the below example,  It is the simplest example of JavaScript.  But hey I have not discussed “language” or “type”, what is it?

Don’t worry, we will talk about it , just after the result of this code:

<html>

<body>

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

<!--

document.write("Welcome to SharpLesson!")

//-->

</script>

</body>

</html>

When you will run above code, then you will see results:

Welcome to SharpLesson

JavaScript syntax – Attributes

As I told you we will discuss “language” and “type” after  the result, so here we go. Javascript has two attributes that we will use most:

  • Language – The language attribute will specify what scripting language you will use in script.
  • Type – It will simply tell that the scripting language is in use.

Note: you can avoid using language attribute, because recent versions of html  will allow you to do that.

One more thing to discuss, remember in the above example, while showing the use of JavaScript  syntax, we used “document.write”. Why we used it?  It is nothing but a function which will be used to write text or Html.

Points to Remember

  • JavaScript is a case sensitive language, and that is why we have covered (case sensitive) in detail.
  • Semicolons are used – if your statements are in different lines, then you can forget to use semicolons, but if you are using more than one statement in a single line, then never forget to put semicolon.
  • You can use // or /* */ to comment in JavaScript.

Conclusion

We have just covered JavaScript syntax and its attributes.  Never forget that syntax will always play a vital role either in language or even in general use. Just practice with a few other examples to get proper understanding, else at sharplesson you will get many options to do that.