JavaScript Syntax

Syntax of Javascript means the set of rules that needs to be followed while writing javascript code. As you learn JavaScript you would be  using terms like script, variables,functions, comments etc. In this section, i will give you a brief intro about these.

Javascript code can be implimented anywhere inside a web page. However it is a good practice to place the code within the HTML head tags. Some developers prefer to keep the JavaScript code at the end of the page to avoid long page load time.

JavaScript can be implemented in two ways

Internal JavaScript – This means you can implement JavaScript code inside a particular page by placing the JavaScript code inside the HTML <script>….</script> tags.

External JavaScript – You can make a separate file of JavaScript and then link that file to your webpage using the  HTML <link> tag.

Case Sensitivity

JavaScript is a case sensitive language. For example, num and NUM are two different variables and not the same.

For Example:

<script>
    var num = 7;
    var NUM = 4;
    document.write(num)
</script>

Output:

7

Comments in JavaScript

Comments in JavaScript are identical to those in C and C++. JavaScript supports single line as well as multiline comments.

A single line comment can be written by placing a text after putting double slash (//). Any text after // upto the end of the line would be treated as a single line comment.

There is also another way to write single line comments, that is by placing the text within <!– and //–>. This is somehow like commenting in HTML.

A multiline comment can be written by putting /* at the start of the comment and */ at the end of the comment.

For Example:

<script>
    //This is a single line comment.
    <!--This is another single line comment//-->
    /*These are multiline comments.
    These are a combination
    of comments.*/
</script>

Text Formatting

JavaScript ignores whitespaces, line breaks and tabs. You are free to indent or format your code. You can even write your code without ending lines or giving whitespaces as long as you follow the coding standards in JavaScript.

You can write your JS code in one line as well!

For Example:

<script>
    var car = "santro";document.write(car);
</script>

Operators in JavaScript

Operators are the symbols that are used to perform operations on different operands. There are different types of operators in JavaScript.

Arithmetic Operators: They are used to perform arithmetic operations on operands.

Assignment Operators: They are used to assign values to variables.

Comparison Operators: They are used to compare values of different variables.

Data Types in JavaScript

There are different datatypes to store different types of values in JavaScript. Since JavaScript is a dynamic language, you need not to specify the type of the variable at the time of declaration.

For Example:

<script>
    //x contain integer value
    var x = 10;
    //city contain string value
    var city = "Indore"
    //arr contain an array
    var arr = ['mango', 'apple', 'orange'];
</script>

JavaScript datatypes can be categorized into two broad categories.

Primitive datatypes – integers/floats, strings, booleans, undefined and null.

Non-primitive datatypes – Objects, arrays and functions

The primitive types are stored by value whereas the non-primitive types are stored by reference.

Note : We shall see the topics such as variables, datatypes, functions, arrays in separate tutorials of this series.