0% found this document useful (0 votes)
7 views

JavaScript is (1)

It is a awesome book

Uploaded by

Bhumika Kumari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

JavaScript is (1)

It is a awesome book

Uploaded by

Bhumika Kumari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

JavaScript is an object-based scripting language which is lightweight and

cross-platform.

JavaScript is not a compiled language, but it is a translated language. The


JavaScript Translator (embedded in the browser) is responsible for translating
the JavaScript code for the web browser.

What is JavaScript
JavaScript (js) is a light-weight object-oriented programming language which
is used by several websites for scripting the webpages. It is an interpreted, full-
fledged programming language that enables dynamic interactivity on
websites when applied to an HTML document. It was introduced in the year
1995 for adding programs to the webpages in the Netscape Navigator browser.
Since then, it has been adopted by all other graphical web browsers. With
JavaScript, users can build modern web applications to interact directly
without reloading the page every time. The traditional website uses js to
provide several forms of interactivity and simplicity.

Although, JavaScript has no connectivity with Java programming language.


The name was suggested and provided in the times when Java was gaining
popularity in the market. In addition to web browsers, databases such as
CouchDB and MongoDB uses JavaScript as their scripting and query language.

Features of JavaScript
There are following features of JavaScript:

1. All popular web browsers support JavaScript as they provide built-in


execution environments.
2. JavaScript follows the syntax and structure of the C programming
language. Thus, it is a structured programming language.
3. JavaScript is a weakly typed language, where certain types are implicitly
cast (depending on the operation).
4. JavaScript is an object-oriented programming language that uses
prototypes rather than using classes for inheritance.
5. It is a light-weighted and interpreted language.
6. It is a case-sensitive language.
7. JavaScript is supportable in several operating systems including,
Windows, macOS, etc.
8. It provides good control to the users over the web browsers.

History of JavaScript
In 1993, Mosaic, the first popular web browser, came into existence. In the year
1994, Netscape was founded by Marc Andreessen. He realized that the web
needed to become more dynamic. Thus, a 'glue language' was believed to be
provided to HTML to make web designing easy for designers and part-time
programmers. Consequently, in 1995, the company recruited Brendan
Eich intending to implement and embed Scheme programming language to
the browser. But, before Brendan could start, the company merged with Sun
Microsystems for adding Java into its Navigator so that it could compete with
Microsoft over the web technologies and platforms. Now, two languages were
there: Java and the scripting language. Further, Netscape decided to give a
similar name to the scripting language as Java's. It led to 'Javascript'. Finally, in
May 1995, Marc Andreessen coined the first code of Javascript named 'Mocha'.
Later, the marketing team replaced the name with 'LiveScript'. But, due to
trademark reasons and certain other reasons, in December 1995, the language
was finally renamed to 'JavaScript'. From then, JavaScript came into existence.

Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:

o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog box,
confirm dialog box and prompt dialog box),
o Displaying clocks etc.

JavaScript Example
1. <script>
2. document.write("Hello JavaScript by JavaScript");
3. </script>

O/P:

Hello JavaScript by JavaScript

JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript
function many times to reuse the code.

Advantage of JavaScript function


Functions are useful in organizing the different parts of a script into the several
tasks that must be completed. There are mainly two advantages of JavaScript
functions.
1. Code reusability: We can call a function several times in a script to
perform their tasks so it saves coding.
2. Less coding: It makes our program compact. We don't need to write
many lines of code each time to perform a common task.

Rules for naming functions:


o It must be case sensitive.
o It must be start with alphabetical character (A-Z) or an underscore symbol.
o It cannot contain spaces.
o It cannot be use as a reserve words.

How to declare a Function:


To declare a function we have to use the reserved keyword "function", followed
by its name, and a set of arguments.

JavaScript Function Syntax


The syntax of declaring function is given below.

1. function functionName([arg1, arg2, ...argN]){


2. //code to be executed
3. }
In the above syntax, function is a reserved keyword and "functionName" is a
name given to the function. JavaScript Functions can have 0 or more
arguments.

JavaScript Function Example


Let's see the simple example of function in JavaScript that does not has
arguments.

1. <script>
2. function msg(){
3. alert("hello! this is message");
4. }
5. </script>
6. <input type=button onclick=msg() value="call function"/>
Test it Now

Output of the above example


Explanation:

In the above given example, we have created a function "msg" which is called
by using the "onclick" event. When a user will click on this button then this
function will be called and an alert box will be displayed. We can reuse the msg
function as many as need in our program, so it will save memory.
JavaScript Function Arguments
We can call function by passing arguments. Let's see the example of function
that has one argument.

1. <script>
2. function getcube(number){
3. alert(number*number*number);
4. }
5. </script>
6. <form>
7. <input type=button value=click onclick=getcube(4) />
8. </form>

In the above example, we created a function, "getcube" having single


parameter this function will be called by using "onclick" event by passing a
value programmatically. When a user will click on button then this function will
be called and an alert box will be displayed with calculating the cube of a given
number which is 4 in above example.

Function with Return Value


We can call function that returns a value and use it in our program. Let's see
the example of function that returns value.

1. <script>
2. function getInfo(){
3. return "Hello Javatpoint! How are you?";
4. }
5. </script>
6. <script>
7. document.write(getInfo());
8. </script>
Test it Now

Output of the above example


Hello Javatpoint! How are you?

Explanation:

In the above example, we created a function, "getinfo" which will be called


automatically when document loaded. Function will return a string "Hello
Javatpoint! How are you?" and "document.write" function will help to write the
returned string on the document.

JavaScript Function Object


In JavaScript, the purpose of Function constructor is to create a new Function
object. It executes the code globally. However, if we call the constructor directly,
a function is created dynamically but in an unsecured way.

Syntax
1. new Function ([arg1[, arg2[, ....argn]],] functionBody)

Parameter
arg1, arg2, .... , argn - It represents the argument used by function.

functionBody - It represents the function definition.

JavaScript Function Methods


Let's see function methods with description.

Method Description

It is used to call a function contains th


apply()
single array of arguments.

bind() It is used to create a new function.

It is used to call a function contains th


call()
argument list.
toString() It returns the result in a form of a strin

JavaScript Function Object Examples

Example 1
Let's see an example to display the sum of given numbers.

1. <script>
2. var add=new Function("num1","num2","return num1+num2");
3. document.writeln(add(2,5));
4. </script>
Test it Now

Output:

Example 2
Let's see an example to display the power of provided value.

1. <script>
2. var pow=new Function("num1","num2","return Math.pow(num1,num2)");
3. document.writeln(pow(2,3));
4. </script>
Test it Now

Output:

JavaScript Objects
A javaScript object is an entity having state and behavior (properties and
method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.

JavaScript is an object-based language. Everything is an object in JavaScript.


JavaScript is template based not class based. Here, we don't create class to get
the object. But, we direct create objects.

Creating Objects in JavaScript


There are 3 ways to create objects.

1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)

1) JavaScript Object by object literal


The syntax of creating object using object literal is given below:

1. object={property1:value1,property2:value2.....propertyN:valueN}
As you can see, property and value is separated by : (colon).

Let’s see the simple example of creating object in JavaScript.

1. <script>
2. emp={id:102,name:"Shyam Kumar",salary:40000}
3. document.write(emp.id+" "+emp.name+" "+emp.salary);
4. </script>
Test it Now

Output of the above example


102 Shyam Kumar 40000

2) By creating instance of Object


The syntax of creating object directly is given below:

1. var objectname=new Object();


Here, new keyword is used to create object.

Let’s see the example of creating object directly.

1. <script>
2. var emp=new Object();
3. emp.id=101;
4. emp.name="Ravi Malik";
5. emp.salary=50000;
6. document.write(emp.id+" "+emp.name+" "+emp.salary);
7. </script>
Test it Now

Output of the above example


101 Ravi 50000

3) By using an Object constructor


Here, you need to create function with arguments. Each argument value can
be assigned in the current object by using this keyword.

The this keyword refers to the current object.

The example of creating object by object constructor is given below.

1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6. }
7. e=new emp(103,"Vimal Jaiswal",30000);
8.
9. document.write(e.id+" "+e.name+" "+e.salary);
10. </script>
Test it Now

Output of the above example


103 Vimal Jaiswal 30000

Defining method in JavaScript Object


We can define method in JavaScript object. But before defining method, we
need to add property in the function with same name as method.

The example of defining method in object is given below.

1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6.
7. this.changeSalary=changeSalary;
8. function changeSalary(otherSalary){
9. this.salary=otherSalary;
10. }
11. }
12. e=new emp(103,"Sonoo Jaiswal",30000);
13. document.write(e.id+" "+e.name+" "+e.salary);
14. e.changeSalary(45000);
15. document.write("<br>"+e.id+" "+e.name+" "+e.salary);
16. </script>
Test it Now

Output of the above example


103 Sonoo Jaiswal 30000
103 Sonoo Jaiswal 45000

JavaScript Object Methods


The various methods of Object are as follows:

S.No Methods Description

This method is us
enumerable and
1 Object.assign()
from a source ob
object

This method is us
2 Object.create() new object with t
prototype object

This method is us
3 Object.defineProperty() some behavioral
property.

This method is us
4 Object.defineProperties() configure multip
properties.

This method retu


5 Object.entries()
with arrays of the
This method prev
6 Object.freeze()
properties from b

This method retu


7 Object.getOwnPropertyDescriptor() descriptor for the
property of the sp

This method retu


8 Object.getOwnPropertyDescriptors() property descript
object.

This method retu


9 Object.getOwnPropertyNames() all properties (en
not) found.

This method retu


10 Object.getOwnPropertySymbols()
all own symbol k

This method retu


11 Object.getPrototypeOf()
prototype of the

This method dete


12 Object.is() whether two valu
value.

This method dete


13 Object.isExtensible()
object is extensib

This method dete


14 Object.isFrozen()
object was frozen

This method dete


15 Object.isSealed()
object is sealed.

This method retu


16 Object.keys() given object's ow
names.
This method is us
17 Object.preventExtensions()
any extensions of

This method prev


properties from b
18 Object.seal()
marks all existing
non-configurable

This method sets


19 Object.setPrototypeOf() of a specified obj
object.

This method retu


20 Object.values()
values.

JavaScript Objects
A javaScript object is an entity having state and behavior (properties and
method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.

JavaScript is an object-based language. Everything is an object in JavaScript.

JavaScript is template based not class based. Here, we don't create class to get
the object. But, we direct create objects.

Creating Objects in JavaScript


There are 3 ways to create objects.

1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)

1) JavaScript Object by object literal


The syntax of creating object using object literal is given below:
1. object={property1:value1,property2:value2.....propertyN:valueN}
As you can see, property and value is separated by : (colon).

Let’s see the simple example of creating object in JavaScript.

1. <script>
2. emp={id:102,name:"Shyam Kumar",salary:40000}
3. document.write(emp.id+" "+emp.name+" "+emp.salary);
4. </script>
Test it Now

Output of the above example


102 Shyam Kumar 40000

2) By creating instance of Object


The syntax of creating object directly is given below:

1. var objectname=new Object();


Here, new keyword is used to create object.

Let’s see the example of creating object directly.

1. <script>
2. var emp=new Object();
3. emp.id=101;
4. emp.name="Ravi Malik";
5. emp.salary=50000;
6. document.write(emp.id+" "+emp.name+" "+emp.salary);
7. </script>
Test it Now

Output of the above example


101 Ravi 50000

3) By using an Object constructor


Here, you need to create function with arguments. Each argument value can
be assigned in the current object by using this keyword.

The this keyword refers to the current object.

The example of creating object by object constructor is given below.

1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6. }
7. e=new emp(103,"Vimal Jaiswal",30000);
8.
9. document.write(e.id+" "+e.name+" "+e.salary);
10. </script>
Test it Now

Output of the above example


103 Vimal Jaiswal 30000

Defining method in JavaScript Object


We can define method in JavaScript object. But before defining method, we
need to add property in the function with same name as method.

The example of defining method in object is given below.

1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6.
7. this.changeSalary=changeSalary;
8. function changeSalary(otherSalary){
9. this.salary=otherSalary;
10. }
11. }
12. e=new emp(103,"Sonoo Jaiswal",30000);
13. document.write(e.id+" "+e.name+" "+e.salary);
14. e.changeSalary(45000);
15. document.write("<br>"+e.id+" "+e.name+" "+e.salary);
16. </script>
Test it Now

Output of the above example


103 Sonoo Jaiswal 30000
103 Sonoo Jaiswal 45000

JavaScript Object Methods


The various methods of Object are as follows:
JavaScript Prototype Object
JavaScript is a prototype-based language that facilitates the objects to acquire
properties and features from one another. Here, each object contains a
prototype object.

In JavaScript, whenever a function is created the prototype property is added


to that function automatically. This property is a prototype object that holds a
constructor property.

Syntax:
1. ClassName.prototype.methodName

What is the requirement of a prototype object?


Whenever an object is created in JavaScript, its corresponding functions are
loaded into memory. So, a new copy of the function is created on each object
creation.

In a prototype-based approach, all the objects share the same function. This
ignores the requirement of creating a new copy of function for each object.
Thus, the functions are loaded once into the memory.

Prototype Chaining
In JavaScript, each object contains a prototype object that acquires properties
and methods from it. Again an object's prototype object may contain a
prototype object that also acquires properties and methods, and so on. It can
be seen as prototype chaining.

JavaScript Prototype Object Example 1


Let's see an example to add a new method to the constructor function.
Output:

Martin Roy
Duke William
Output:

Martin Roy Javatpoint


Duke William Javatpoint

You might also like