JavaScript is (1)
JavaScript is (1)
cross-platform.
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.
Features of JavaScript
There are following features of JavaScript:
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:
JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript
function many times to reuse the code.
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
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>
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
Explanation:
Syntax
1. new Function ([arg1[, arg2[, ....argn]],] functionBody)
Parameter
arg1, arg2, .... , argn - It represents the argument used by function.
Method Description
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.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
1. object={property1:value1,property2:value2.....propertyN:valueN}
As you can see, property and value is separated by : (colon).
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
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
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
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
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.
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 template based not class based. Here, we don't create class to get
the object. But, we direct 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. <script>
2. emp={id:102,name:"Shyam Kumar",salary:40000}
3. document.write(emp.id+" "+emp.name+" "+emp.salary);
4. </script>
Test it Now
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
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
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
Syntax:
1. ClassName.prototype.methodName
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.
Martin Roy
Duke William
Output: