Comments and Objects in JavaScript
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
- Single Line Comment
- Multi-line comment
Single Line Comment
- <!DOCTYPE html>
- <html>
- <title>JavaScript Comments</title>
- <head></head>
- <body>
- <script language="javascript">
- //Single line comment
- //Addition of two numbers
- var a = 25;
- var b = 75;
- var c = a + b; //addition of a and b is store in c variable
- document.write("Addition of " + a + " and " + b + " is " + c);
- </script>
- </body>
- </html>
Multi-line Comment
- <!DOCTYPE html>
- <html>
- <title>JavaScript Comments</title>
- <head></head>
- <body>
- <script language="javascript">
- /*
- var a = 25;
- var b = 75;
- var c = a + b; //addition of a and b is store in c variable
- document.write("Addition of " + a + " and " + b + " is " + c);
- */
- function Print() {
- document.write("Example of Multi-line Comments in JavaScript");
- }
- Print();
- </script>
- </body>
- </html>
Objects in JavaScript
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.)
- 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(); //Creating an object
- //Assigning properties to an object 'employee'
- employee.name = "Jeetendra";
- employee.city = "Pune";
- //Assigning method to an object 'employee'
- employee.info = function () {
- document.write("Name : " + this.name + " City : " + this.city);
- }
- //calling the method
- 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.
You can delete properties from objects.
Syntax
Deleting Properties
- 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(); //Creating an object
- //Assigning properties to an object 'employee'
- employee.name = "Jeetendra";
- employee.city = "Pune";
- employee.id = "1072";
- employee.company = "GNS";
- //deleting properties of object 'employee'
- delete employee.company;
- //Assigning method to an object 'employee'
- employee.info = function () {
- document.write("Name : " + this.name + " City : " + this.city);
- document.write("ID : " + this.id + " Company : " + this.company);
- }
- //calling the method
- employee.info();
- </script>
- </body>
- </html>
Built-in Objects
- Math Objects
- Date Objects
- String Objects
Math Objects
- Math.methodName(value);
Example
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
- //Math object example
- var n = 4;
- var res = Math.sqrt(n);
- document.write("Square of " + n + " is " + res);
- </script>
- </body>
- </html>
Date Objects
- dateObject.methodName();
Example
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
- //Date object example
- var date = new Date();
- var res = date.getFullYear();
- document.write("Current Date is " + date + " and Year is " + res);
- </script>
- </body>
- </html>
String Objects
Syntax
- stringName.methodName();
Example
- <!DOCTYPE html>
- <html>
- <title>JavaScript Objects</title>
- <head></head>
- <body>
- <script language="javascript">
- //String object example
- var str = "C# Corner";
- var bld = str.bold();
- document.write("First String is " + str + " After formatting " + bld);
- </script>
- </body>
- </html>
Summary
Author
Jeetendra Gund
48
30.7k
3.1m