JavaScript Syntax
JavaScript syntax refers to a set of rules that determine how the language will be written (by the programmer) and interpreted (by the browser).
The JavaScript syntax is loosely based on the Java syntax. Java is a full blown programming environment and JavaScript could be seen as a sub-set of the Java syntax. Having said this, that is where the similarities end — Java and JavaScript are two totally different things.
By learning JavaScript you will become familiar with terms such as variables, functions, statements, operators, data types, objects etc.
It would take much more than a short tutorial to cover the complete JavaScript syntax. However, this tutorial covers the syntax basics that will enable you to code JavaScript in a proficient manner.
For now, I'll give you a quick intro by showing you an example and explanation.
Example code
The above example is how you write text to a web page using JavaScript.
Explanation of code
-
The HTML
<script>
tags tell the browser to expect a script in between them.You can also specify the language by adding
type="text/javascript"
. However, from HTML5, JavaScript is the default language, so this attribute is optional if you're using Javascript. The part that writes the actual text is only 1 line:
This is one way of writing text to a web page in JavaScript. This is an example of using a JavaScript function (also known as a method).
Where to put your scripts?
You can place your scripts in any of the following locations:
- Between the HTML document's
head
tags. - Within the HTML document's body (i.e. between the
body
tags). - In an external file (and link to it from your HTML document).