JavaScript Tutorial – Syntax

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program. Let’s look at the example below:

<script type="text/javascript">
   document.write("Hello World!");
</script>

JavaScript ignores spaces and line breaks, so you are free to format and indent your code in a neat and consistent way that makes the code easy to read and understand.

JavaScript Statement

Statements are the instructions for the computer to execute and they are separated by semicolons. Semicolons are optional if each of the statements are placed on a separate line, but it is recommended that you end a statement with a semicolon, it is just much cleaner to read.

There are two types of JavaScript values

Fixed values are called JavaScript literals. Examples are numbers and strings.Numbers are written with or without decimals, and Strings are text, written within double or single quotes.

<script type="text/javascript">
   10
   10.03
   "What About HTML"
   'Tutorial for beginner'
</script>

Variable values are called variables. JavaScript variables are used to store data values and they are defined with the var keyword. An equal sign is used to assign values to variables.

<script type="text/javascript">
   var website = "WhatAboutHTML";
</script>

in this example, website is defined as a variable and it stores the value WhatAboutHTML

What is JavaScript keyword

Keywords are reserved words and cannot be used as variables or function names. They are used to identify actions to be performed. In the above example, keyword var tells the browsers to create a new variable.

What is Camel Case

Camel Case is the practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter. With JavaScript, it is highly recommended that you start with a lowercase letter. See below examples.

<script type="text/javascript">
   var myName;
   var myCarColor;
</script>

JavaScript is case sensitive

This is very important, variable mycarcolor is not the same as myCarColor. JavaScript sees them as two different variables.

<script type="text/javascript">
   var mycarcolor = "red";
   var myCarColor = "blue";
</script>