Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaScript

JavaScript, originally developed as LiveScript in 1995, is a lightweight, object-oriented, client-side scripting language that enhances web interactivity. It allows for dynamic effects in HTML documents and is widely used in game and mobile application development. Key features include case sensitivity, variable declaration, and the ability to define global and local variables, with the 2015 version introducing 'const' and 'let' for variable definitions.

Uploaded by

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

JavaScript

JavaScript, originally developed as LiveScript in 1995, is a lightweight, object-oriented, client-side scripting language that enhances web interactivity. It allows for dynamic effects in HTML documents and is widely used in game and mobile application development. Key features include case sensitivity, variable declaration, and the ability to define global and local variables, with the 2015 version introducing 'const' and 'let' for variable definitions.

Uploaded by

s41663117
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript

JavaScript was originally developed as LiveScript by Brendan Eich in 1995,


which appeared in Netscape, a popular browser of that time.. It was later
renamed to JavaScript in 1995.
JavaScript (js) is a light-weight object-oriented, open-source and most popular
client-side scripting language supported by all browsers. Client-side scripting
refers to scripts that run within your web browser. JavaScript is designed to
add interactivity and dynamic effects to the websites when applied to an HTML
document. In other words, you can make your webpage more lively and
interactive, with the help of JavaScript. It is an interpreted language. JavaScript
is also being used widely in game development and Mobile application
development.
Syntax

JavaScript can be implemented using JavaScript statements that are placed


within the <script>... </script> HTML tags in a web page.
You can place the <script> tags, containing your JavaScript, anywhere within
your web page. The <script> tag alerts the browser program to start
interpreting all the text between these tags as a script.Scripts can be placed in
the <body>, or in the <head> section of an HTML page, or in both.

A simple syntax of your JavaScript will appear as follows.


<script ...>
JavaScript code
</script>
OR
<script language = "javascript" type = "text/javascript">
JavaScript code
</script>

The script tag takes two important attributes −


 Language − This attribute specifies what scripting language you are
using. Typically, its value will be javascript..
 Type − its value should be set to "text/javascript".

Simple Java script program


Example1
<html>
<head>
<title>My First JavaScript code!!!</title>
<script>
alert("Hello World!");
</script>
</head>
<body>
</body>
</html>

 Case Sensitivity
JavaScript is a case-sensitive language. This means that the language
keywords, variables, function names, and any other identifiers must always be
typed with a consistent capitalization of letters.
So the identifiers Time and TIME will convey different meanings in JavaScript.
Note:JavaScript ignores spaces, tabs, and newlines that appear in JavaScript
programs
 Comments in JavaScript
 Any text between a // and the end of a line is treated as a comment and
is ignored by JavaScript.(used for single line comment)
 Any text between the characters /* and */ is treated as a comment. This
may span multiple lines. (used for multiline line comment)
 JavaScript also recognizes the HTML comment opening sequence <!--.
JavaScript treats this as a single-line comment, just as it does the //
comment.
 The HTML comment closing sequence --> is not recognized by JavaScript
so it should be written as //-->.

 Semicolons are Optional


Simple statements in JavaScript are generally followed by a semicolon
character, just as they are in C, C++, and Java. JavaScript, however, allows you
to omit this semicolon if each of your statements are placed on a separate
line. For example, the following code could be written without semicolons.
<script language = "javascript" type = "text/javascript">
<!--
var1 = 10
var2 = 20
//-->
</script>
But when formatted in a single line as follows, you must use semicolons −
<script language = "javascript" type = "text/javascript">
<!--
var1 = 10; var2 = 20;
//-->
</script>

 To display data
JavaScript can "display" data in different ways:
 Writing into an HTML element, using innerHTML.
 Writing into the HTML output using document.write().
 Writing into an alert box, using window.alert().
 Writing into the browser console, using console.log().

 JavaScript Variables
JavaScript variables are containers for storing data values.
Variables are used to store values (name = "John") or expressions (sum = x + y).
JavaScript is untyped language. This means that a JavaScript variable can hold
a value of any data type. Unlike many other languages, you don't have to tell
JavaScript during variable declaration what type of value the variable will hold.
The value type of a variable can change during the execution of a program and
JavaScript takes care of it automatically.

Declare Variables in JavaScript


Before using a variable, you first need to declare it. You have to use the
keyword var to declare a variable like this:
var name;

Assign a Value to the Variable


You can assign a value to the variable either while declaring the variable or
after declaring the variable.
var name = "John";

OR

var name;
name = "John";

You can declare many variables in one statement.


Start the statement with var and separate the variables by comma:
var person = "John Doe", carName = "Volvo", price = 200;

JavaScript Variable Names


While naming your variables in JavaScript, keep the following rules in mind.
 A variable name must start with a letter, underscore (_), or dollar sign
($).
 A variable name cannot start with a number..
 A variable name cannot contain spaces.
 A variable name cannot be a JavaScript keyword or a JavaScript reserved
word.
 JavaScript variable names are case-sensitive. For
example, Name and name are two different variables.

JavaScript Variable Scope


The scope of a variable is the region of your program in which it is defined.
JavaScript variables have only two scopes.

1.Global Variables
A global variable has global scope which means it can be defined anywhere in
your JavaScript code.
The scope of global variables is throughout the script, it means it is available to
all the functions of a program and can be accessed from anywhere in the
script.
Example.

<html>
<body>
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();

</script>
</body>
</html>

2.Local Variables
A variable that is declared and used inside the function or block is called a local
variable. Its scope is limited to only that function or block in which it is
declared. It cannot be used outside the block..A local variable will be visible
only within a function where it is defined. Function parameters are always
local to that function.

Example.

<script>
function abc(){
var x=10;//local variable
}
</script>
Within the body of a function, a local variable takes precedence over a global
variable with the same name. If you declare a local variable or function parameter
with the same name as a global variable, you effectively hide the global variable.
Example.

<html>
<body onload = checkscope();>
<script type = "text/javascript">
<!--
var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
document.write(myVar);
}
//-->
</script>
</body>
</html>

OUTPUT:
local
Note: 2015 version of JavaScript allows the use of the const and let keyword to
define a variable.

You might also like