[Go to site: main page, start]

0% found this document useful (0 votes)
3 views9 pages

Simple JavaScript, Comments, Variables

The document provides an overview of JavaScript, including where to place JavaScript code (within body, head, or as an external file), and demonstrates simple examples such as using document.write() and alert(). It also explains JavaScript comments, their advantages, and types, as well as the concept of variables, distinguishing between local and global variables with examples. Overall, it serves as a basic guide for beginners in JavaScript programming.
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)
3 views9 pages

Simple JavaScript, Comments, Variables

The document provides an overview of JavaScript, including where to place JavaScript code (within body, head, or as an external file), and demonstrates simple examples such as using document.write() and alert(). It also explains JavaScript comments, their advantages, and types, as well as the concept of variables, distinguishing between local and global variables with examples. Overall, it serves as a basic guide for beginners in JavaScript programming.
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

Simple JavaScript

 JavaScript example is easy to code. JavaScript


provides three places to put the JavaScript code:
within body tag, within head tag and external
JavaScript file.
 Example:
<script type= “text/javascript”>
[Link](“JavaScript is a Simple Language”);
</script>
The script tag specified that we are using JavaScript.
The text/javascript is the content type that
provides information to the browser about data.
The [Link]() function is used to display
dynamic content through JavaScript.

WEB DESIGNING
II [Link]., CS
Three places to put JavaScript code:
[Link] Example: Code between the body tag
In the above example, we have displayed the
dynamic content using JavaScript. Let’s see the
simple example of JavaScript that displays alert
dialog box.
<script type=”text/javascript”>
Alert(“Hello JavaScript”);
</script >
[Link] Example: Code between the head tag
We are creating a function msg(). To create function
in JavaScript, you need to write function with
function_name as given below. To call function, you
need to work on event. Here we are using on click
event to call msg() function.
<html>

2
<head>
<script type=”text/javascript”>
function msg() {
alert(“Hello JavaScript”);
}
</script>
</head>
<body>
<p> Welcome to JavaScript</p>
<form>
<input type =”button” value = “click” onclick
=”msg()”/>
</form>
</body>
</html>

3
[Link] JavaScript file
We can create external JavaScript file and embed it
in many html page. It provides code re – usability
because single JavaScript file can be used in several
html pages. At external JavaScript file must be
saved by js execution. It is recommended to embed
all javascript files into a single file. It increase the
speed of the webpage. Let’s create an external
JavaScript.
message .js
function msg(){
alert(“Hello JavaScript “);
}
Let’s include the JavaScript file into html page. It
calls the JavaScript function on button click.
[Link]

4
<html>
<head>
<script type=”text/javascript “ STC =”[Link] “>
</script>
<body>
<p>Welcome to JavaScript</p>
</form>
<input type = “button” value = “click” >msg()”/ >
</form>
</body>
</html>

5
JavaScript Comments

 In JavaScript comments are meaningful way to


deliver message. It is used to add information
about the code, warnings or suggestions, so that
end user can easily interpret the code.
 The Javascript comment is ignored by the
Javascript engine i.e. embedded in the browser.
Advantages of JavaScript comments
There are mainly two advantages of JS comments.
[Link] make code easy to understand.
[Link] avoid the unnecessary code.
Types of JavaScript comments
[Link] – line comments
[Link] – line comments

6
JavaScript single line comment
It is represented by double forward slashes (//). It
can be used before and
after the statement.
<script>
// It is single line comment
[Link](“Hello JavaScript”);
</script>
JavaScript multi line comment
It can be used to add single as well as multi line
comments. So it is more convenient.
/*Your code here */

7
JavaScript Variables

 A JavaScript Variable is simply a name storage


location. There are two types of variables in JS:
Local variable and Global variable.
Rules for declaration in variable
[Link] must start with a letters ( a to z or A to z),
underscore (_) , or dollar ($) sign.
[Link] first letter we can use digits ( 0 to 9). For
example value 1.
[Link] variables are case sensitive
JavaScript local variable
A JavaScript local variable is declared inside block or
function. It is accessible within the function or block
only.
<script>

8
function abc(){
var x=10; //local variable
}
</script>
JavaScript global variable
A JavaScript global variable is accessible from any
function. A variable i.e. declared outside the
function or declared with windows object is known
as global variable.
<script>
var data = 200; //global variable
function a(){
}
</script>

You might also like