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

Introduction to JavaScript Basics

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

Introduction to JavaScript Basics

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

JAVASCRIPT

JavaScript is the scripting language of the Web. JavaScript is used in millions of Web pages to add
functionality, validate forms, detect browsers, and much more.

Introduction to JavaScript

JavaScript is used in millions of Web pages to improve the design, validate forms, detect browsers,
create cookies, and much more.

JavaScript is the most popular scripting language on the Internet, and works in all major browsers,
such as Internet Explorer, Mozilla Firefox, and Opera.

What is JavaScript?

 JavaScript was designed to add interactivity to HTML pages

 JavaScript is a scripting language

 A scripting language is a lightweight programming language

 JavaScript is usually embedded directly into HTML pages

 JavaScript is an interpreted language (means that scripts execute without preliminary compilation)

 Everyone can use JavaScript without purchasing a license

JavaScript Variables:

Declaring (Creating) JavaScript Variables Creating variables in JavaScript is most often referred to as
"declaring" variables.

You can declare JavaScript variables with the let statement:

Example:

let x;

Assigning Values to JavaScript Variables

x=5;

JavaScript Functions

A function (also known as a method) is a self-contained piece of code that performs a particular
"function". You can recognise a function by its format - it's a piece of descriptive text, followed by
open and close brackets.

A function is a reusable code-block that will be executed by an event, or when the function is
called. To keep the browser from executing a script when the page loads, you can put your script
into a function.

A function contains code that will be executed by an event or by a call to that function.
How to Define a Function:

The syntax for creating a function is:

function functionname()

some code

What is an Event?

Event Handlers

Event Handlers are JavaScript methods, i.e. functions of objects, that allow us as JavaScript
programmers to control what happens when events occur.

Directly or indirectly, an Event is always the result of something a user does. For example, we've
already seen Event Handlers like onClick and onMouseOver that respond to mouse actions.

Alert box :

The alert() method displays an alert box with a message and an OK button.

Syntax
alert(message)

Example 1
<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>


<p>My first paragraph.</p>

<script>
[Link](5 + 6);
</script>

</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>


<p>My first paragraph.</p>

<script>
alert(5 + 6);
</script>

</body>
</html>

Example 3

Example :

<html>

<body>

<h1>The Window Object</h1>

<h2>The alert() Method</h2>

<p>Click the button to display an alert box.</p>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction() {

alert("Hello! I am an alert box!");

</script>

</body>

</html>

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after
entering an input value.

Example:
<!DOCTYPE html>
<html>
<body>

<h1>The Window Object</h1>


<h2>The prompt() Method</h2>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>


<script>
function myFunction() {
let person = prompt("Please enter your name", "Harry Potter");
alert("Welcome "+person)
}
</script>
</body>
</html>

You might also like