JavaScript Syntax

Understanding the JavaScript Syntax

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.

A JavaScript consists of JavaScript statements that are placed within the <script></script> HTML tags in a web page, or within the external JavaScript file having .js extension.

The following example shows how JavaScript statements look like:

Example

let x = 5;
let y = 10;
let sum = x + y;
document.write(sum); // Prints variable value

You will learn what each of these statements means in upcoming chapters.


Case Sensitivity in JavaScript

JavaScript is case-sensitive. This means that variables, language keywords, function names, and other identifiers must always be typed with a consistent capitalization of letters.

For example, the variable myVar must be typed myVar not MyVar or myvar. Similarly, the method name getElementById() must be typed with the exact case not as getElementByID().

Example

let myVar = "Hello World!";
console.log(myVar);
console.log(MyVar);
console.log(myvar);

If you checkout the browser console by pressing the f12 key on the keyboard, you’ll see a line something like this: “Uncaught ReferenceError: MyVar is not defined”.


JavaScript Comments

A comment is simply a line of text that is completely ignored by the JavaScript interpreter. Comments are usually added with the purpose of providing extra information pertaining to source code. It will not only help you understand your code when you look after a period of time but also others who are working with you on the same project.

JavaScript support single-line as well as multi-line comments. Single-line comments begin with a double forward slash (//), followed by the comment text. Here’s an example:

Example

// This is my first JavaScript program
document.write("Hello World!");

Whereas, a multi-line comment begins with a slash and an asterisk (/*) and ends with an asterisk and slash (*/). Here’s an example of a multi-line comment.

Example

/* This is my first program 
in JavaScript */
document.write("Hello World!");

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *