DEV Community

elinabey
elinabey

Posted on

Basic Syntax of JavaScript

Image description
JavaScript can be implemented using JavaScript statements that are placed within the <script>... </script> HTML tags in a web page.

You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags.

The <script> tag alerts the browser program to start interpreting all the text between these tags as a script. A simple syntax of your JavaScript will appear as follows.

<script> 
JavaScipt Code like below.
document.write("Basic Print method in JavaScript"); 
</script> 
Enter fullscreen mode Exit fullscreen mode

Basic JavaScript Code:

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

This function can be used to write text, HTML, or both. Take a look at the following code.

<html>
   <body>   
      <script language = "javascript" type = "text/javascript">
         <!--
            document.write("The Dev Team!")
         //-->
      </script>      
   </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output:
The Dev Team!

Want to learn in Details, you may read in details from Basic Syntax of JavaScript here.

If you have any question to improve this post let's discuss below. Thank you.

Top comments (0)