Welcome to another interesting tutorial. Here you will learn how to write the JavaScript code.
JavaScript syntax is a set of rules that define a well-structured JavaScript program.
The syntax of a JavaScript consists of JavaScript statements that are position within the ‘<script></script> HTML’ tags in a web page, or it could be within the external JavaScript file having the .js extension.
Let's look at the examples to see how JavaScript statements look like:
Example :
var x = 5;
var y = 10;
var sum = x + y;
document.write(sum); // Prints variable value
In the next chapters, you will get to understand what each of these statements means.
Case Sensitivity in JavaScript
JavaScript is a case-sensitive programming language. As such, the use of variables language keywords, function names and other identifies must be consistent in terms of capitalization of letters.
Let’s take look at this example, the variable MyVar must be typed as MyVar and not as myvar or Myvar. Also, the method name getElementById() should be typed with the same case, and not as Get element ById().
Example :
var myVar = "Hello World!";
console.log(myVar);
console.log(MyVar);
console.log(myvar);
To see the error response, check out your browser console by tapping the f12 key on the keyboard, you will see a line statement like this ‘Uncaught ReferenceError: myvar is not defined’.
Now, let's understand what JavaScript comment is about.
JavaScript comment is a line statement or text that is completely ignored by the JavaScript interpreter. This line statement or comments are usually added in codes to provide additional information about the code. This additional information will help others who are working with you on the same project to understand what you are doing or have done, and it also helps you as well in better understanding your coding.
As you may know, JavaScript supports single-line as well as multi-line comments. In programming, Single-line comments begin with a double forward slash ‘//’, followed by the comment text. Below is an example
Example :
// This is my first JavaScript program
document.write("Hello World!");
While a multi-line comment starts with a slash and an asterisk ‘/*’ and ends with an asterisk and slash ‘*/’. This is shown below.
Example :
/* This is my first program
in JavaScript */
document.write("Hello World!");