0% found this document useful (0 votes)
2 views5 pages

Javascript

JavaScript is a lightweight, object-oriented scripting language primarily used for creating dynamic web content and controlling multimedia. It differs from Java in that it is more flexible with variable declarations and does not require a class-based structure. Programming, in general, involves manipulating data and performing calculations, with JavaScript enabling interaction through the Document Object Model (DOM) for web pages.

Uploaded by

bijeshsharma2016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Javascript

JavaScript is a lightweight, object-oriented scripting language primarily used for creating dynamic web content and controlling multimedia. It differs from Java in that it is more flexible with variable declarations and does not require a class-based structure. Programming, in general, involves manipulating data and performing calculations, with JavaScript enabling interaction through the Document Object Model (DOM) for web pages.

Uploaded by

bijeshsharma2016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

What is JavaScript?

JavaScript is a cross-platform, object-oriented scripting language. It is a small and


lightweight language Inside a host environment (for example, a web browser),
JavaScript can be connected to the objects of its environment to provide
programmatic control over them.

JavaScript contains a standard library of objects, such as Array, Date, and Math,
and a core set of language elements such as operators, control structures, and
statements.

So finally, JavaScript is a scripting language that enables you to create dynamically


updating content, control multimedia, animate images, and pretty much
everything else.

Difference between JavaScript and Java


JavaScript Java

Object-oriented. No distinction Class-based. Objects are divided into


between types of objects. Inheritance is classes and instances with all
through the prototype mechanism, and inheritance through the class hierarchy.
properties and methods can be added Classes and instances cannot have
to any object dynamically. properties or methods added
dynamically.

Variable data types are not declared Variable data types must be declared
(dynamic typing). (static typing).

Cannot automatically write to hard Can automatically write to hard disk.


disk.

JavaScript is a very free-form language compared to Java. You do not have to


declare all variables, classes, and methods. You do not have to be concerned with
whether methods are public, private, or protected, and you do not have to
implement interfaces. Variables, parameters, and function return types are not
explicitly typed.

Java is a class-based programming language designed for fast execution and type
safety. Type safety means, for instance, that you can't cast a Java integer into an
object reference or access private memory by corrupting Java bytecodes. Java's
class-based model means that programs consist exclusively of classes and their
methods. Java's class inheritance and strong typing generally require tightly
coupled object hierarchies. These requirements make Java programming more
complex than JavaScript programming.

What Is Programming About?


programming is largely about performing different types of calculations upon
various types of data (including numbers, text and graphics). In all programming
languages.

You can perform tasks such as:

 Performing mathematical calculations on numbers such as addition,


subtraction, multiplication and division.
 Working with text to find out how long a sentence is, or where the first
occurrence of a specified letter is within a section of text.
 Checking if one value (numbers or letters) matches another.
 Checking if one value is shorter or longer, lower or higher than another.
 Performing different actions based on whether a condition (or one of
several conditions) is met. For example, if a user enters a number less than
10, a script or program can perform one action; otherwise it will perform a
different action.
 Repeating an action a certain number of times or until a condition is met
(such as a user pressing a button).

In object - oriented programming languages, real - life objects are represented (or
modeled) using a set of objects , which form an object model . For example, a car
object might represent a car, a basket object might represent a shopping basket,
and a document object could represent a document such as a web page.
Each object can have a set of properties that describes aspects of the object. A car
object might have properties that describe its color or engine size. A basket object
might have properties that describe the number of items it contains or the value
of those items. A document object has properties that describe the background
color of the web page or the title of the page.

Then there are methods, each method describes an action that can be done to (or
with) the object. For example, a method of an object representing a car might be
to accelerate, or to change gear. A method of a shopping basket might be to add
an item, or to recalculate the value of items in the basket. A method on a
document could be to write a new line of text into the web page.

Finally, there are events, in a programming language, an event is the object


putting up its hand and saying “ x just happened, ” usually because a program
might want to do something as a result of the event. For example, a car object
might raise an event to say the ignition started, or that it is out of fuel. A basket
might raise an event when an item is added to it, or when it is full. A document
might raise an event when the user presses Submit on a form, or clicks a link.
Furthermore, an event can also trigger actions; for example, if a car is out of fuel
then the car will stop.

All of the main browsers implement an object model called the Document Object
Model that was devised to represent web pages. In Document Object Model, the
page as a whole is represented using a document object; then the links in that
page are represented using a links object, the forms are represented in a forms
object, images are represented using an image object, and so on.

So, the Document Object Model describes how you can:

 Get and set properties of a web page such as the background color.
 Call methods that perform actions such as writing a new line into a page.
 React to events such as a user pressing a Submit button on a form.

How to Add a Script to Your Pages


JavaScript can either be embedded in a page or placed in an external script file
(rather like CSS). But in order to work in the browser, the browser must have
JavaScript enabled. (The major browsers allow users to disable JavaScript,
although very few people do.)
You add scripts to your page inside the < script > element. The type attribute on
the opening < script > tag indicates what scripting language will be found inside
the element, so for JavaScript you use the value text/JavaScript .

There are several other scripting languages that do a very similar job to JavaScript
(such as VBScript orPerl), but JavaScript is the main programming language used
in web browsers

Example
< html >
< body >
< p >
< script type=”text/javascript” >
document.write(“My first JavaScript”)
< /script >
< /p >
< /body >
< /html >

You can also write JavaScript in external documents that have the file extension .js
(just in the same way that you can write external style sheets). This is a
particularly good option because:

 If your script is used by more than one page you do not need to repeat the
script in each page that uses it.
 If you want to update your script you need only change it in one place.
 It makes the XHTML page cleaner and easier to read.

When you place your JavaScript in an external file, you need to use the src
attribute on the < script > element; the value of the src attribute should be an
absolute or relative URL pointing to the file containing the JavaScript.

For example:

< script type=”JavaScript”


src=”scripts/validation.js” > < /script >
So there are three places where you can put your JavaScripts , and a single XHTML
document can use all three because there is no limit on the number of scripts one
document can contain:
 In the < head > of a page: These scripts will be called when an
event triggers them.
 In the < body > of a page: These scripts will run as the page
loads.
 In an external file: If the link is placed inside the < head > element,
the script is treated the same as when the script lives inside the head of the
document waiting for an event to trigger it, whereas if it is placed in the <
body > element it will act like a script in the body section and execute as
the page loads.

You might also like