Introduction
In the previous chapter, we learned about Client-side vs Server-side Programming Languages and how a client-side programming language works with an example program.
In this chapter, we will learn about some basic concepts of JavaScript used in Web Applications, like comments, objects, and so on.
Comments in JavaScript
In JavaScript comments are used for skipping that statement from execution, using comments you make code more readable and understandable for anyone. Code functionality is clearer using comments. The following comments are used in JavaScript:
- Single Line Comment
- Multi-line comment
Single Line Comment
When you want to comment out a single statement then a single line comment is used. It starts with "//". Using this you can comment out an entire line. This line is ignored by JavaScript.
The following example uses a single line comment to explain the code:
- <!DOCTYPE html>
- <html>
- <title>JavaScript Comments</title>
- <head></head>
- <body>
- <script language="javascript">
-
-
- var a = 25;
- var b = 75;
- var c = a + b;
- document.write("Addition of " + a + " and " + b + " is " + c);
- </script>
- </body>
- </html>
Output: Addition of 25 and 75 is 100
Multi-line Comment
A multi-line comment is used with a group or code block that you want to comment out. A multi-line comment starts with /* and ends with */. The code block between this is skipped by JavaScript.
In the following example, a group of statements is commented out using multi-line comments.
- <!DOCTYPE html>
- <html>
- <title>JavaScript Comments</title>
- <head></head>
- <body>
- <script language="javascript">
-
-
-
-
-
-
- function Print() {
- document.write("Example of Multi-line Comments in JavaScript");
- }
- Print();
- </script>
- </body>
- </html>
Output: Example of Multi-line Comments in JavaScript
Objects in JavaScript
An object is a thing, just like things in the real world. Objects are a way to organize variables. In the web browser objects include the browser window itself, forms and buttons, and so on. (For example cars, books, dogs, money.)
Properties and Methods
Every object has its own properties and methods. Properties define the characteristics of an object. (For example color, name, length.) Methods that can be done on objects.
(For example, alert, confirm, and open.)
The following is the syntax of properties:
objectName.propName or objectName[“property”]
The following is the syntax of methods:
- methodName : function () { Statements; }
Example
The following demo example explains the concept of objects, properties, and methods. In this example, an object is created named "employee". It has the property's name and city. Creating the method for this object employee and call.
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
- var employee = new Object();
-
-
- employee.name = "Jeetendra";
- employee.city = "Pune";
-
-
- employee.info = function () {
- document.write("Name : " + this.name + " City : " + this.city);
- }
-
-
- employee.info();
- </script>
- </body>
- </html>
Output: Name : Jeetendra City : Pune
"this" in JavaScript
An object can be referred to using "this" in JavaScript. It is not a variable, it's a keyword. You cannot set the value for this.
Deleting Properties
You can delete properties from objects.
Syntax - delete objectName.propName;
Example
In the following example, you can understand how to delete properties from objects. Here I deleted the "company" property of the object "employee".
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
- var employee = new Object();
-
-
- employee.name = "Jeetendra";
- employee.city = "Pune";
- employee.id = "1072";
- employee.company = "GNS";
-
-
- delete employee.company;
-
-
- employee.info = function () {
- document.write("Name : " + this.name + " City : " + this.city);
- document.write("ID : " + this.id + " Company : " + this.company);
- }
-
- employee.info();
- </script>
- </body>
- </html>
Output: Name : Jeetendra City : PuneID : 1072 Company : undefined
In the preceding output, you can see that I deleted the property company; its value is printed as "undefined".
Built-in Objects
There are some built-in objects of JavaScript that offer more advanced operations like the following:
- Math Objects
- Date Objects
- String Objects
Math Objects
It provides methods for many mathematical calculations like: abs(), log(), pow(), sqrt() and so on.
Syntax
Example
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
-
- var n = 4;
- var res = Math.sqrt(n);
- document.write("Square of " + n + " is " + res);
- </script>
- </body>
- </html>
Output: Square of 4 is 2
Date Objects
It provides the methods to get the current day, month, and year.
Syntax
Example
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
-
- var date = new Date();
- var res = date.getFullYear();
- document.write("Current Date is " + date + " and Year is " + res);
- </script>
- </body>
- </html>
Output: Current Date is Thu Nov 27, 2014, 23:22:37 GMT+0530 (India Standard Time) and Year is 2014
String Objects
It provides the methods and properties for string manipulation and formatting.
Syntax
Example
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
-
- var str = "C# Corner";
- var bld = str.bold();
- document.write("First String is " + str + " After formatting " + bld);
- </script>
- </body>
- </html>
Output: First String is C# Corner After formatting C# Corner.
Summary
In this article, we learned about some basic concepts of JavaScript used in Web Applications, like comments, objects, and so on.