- 5 years ago
- Zaid Bin Khalid
- 4,138 Views
-
5
In this session, you will learn how to write code in JavaScript with the help of an example. To write JavaScript consider the below methods.
JavaScript provides three places to put the JavaScript code.
- Within the body tag.
- Within the head tag.
- External JavaScript file.
Understanding the JavaScript Syntax
Typically, the syntax of JavaScript is the set of rules.These rules define through correct JavaScript program.
The JavaScript statements which are placed within the <script></script>
HTML tags in a web page, or within the external JavaScript file having .js
extension.
The below example will show how JavaScript statement looks like:
var x = 5;
var y = 10;
var sum = x + y;
document.write(sum); // Prints variable value
Case Sensitivity in JavaScript
JavaScript is case-sensitive, which means the language keywords, variables, function names, and other identifiers must always be written with a consistent letter of capitalization.
Example
You must write the variables as myVar not as MyVar or myvar.
For the method name “getElementById( )” should write exactly the same.
var myVar = "Hello World!";
console.log(myVar);
console.log(MyVar);
console.log(myvar);
If you press F12 key by using the keyboard.
On your browser console, you will receive the following output result:
“Uncaught ReferenceError: MyVar is not defined.”
JavaScript Comments
A comment is simply a line of text, In JavaScript comment entirely ignored by the JavaScript interpreter. The major purpose of adding comments will provide a piece of extra information about source code.
These comments help you and your team in understanding the source code easily. Even after you go through the source code after ling time.
Single-line Comments
The Single-line comments that begin with a (//) double forward slash. A single-line comment example as shown.
// This is my first JavaScript program
document.write("Hello World!");
Multi-line Comments
Typically, in JavaScript, you can use multi-line comments to add extra descriptions. A multi-line comment begins with a (/*) slash and an asterisk and ends with (*/) asterisk and slash.
Example
A multi-line comment example as shown.
/* This is my first program
in JavaScript */
document.write("Hello World!");
- 5 years ago
- Zaid Bin Khalid
- 4,138 Views
-
5