0% found this document useful (0 votes)
5 views

java script day1

JavaScript is a lightweight, object-based scripting language used for creating interactive websites, supported by all major web browsers. It features a syntax similar to C, is case-sensitive, and allows for both internal and external script declarations. JavaScript supports various data types, operators, and enables code reusability through functions and external files.

Uploaded by

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

java script day1

JavaScript is a lightweight, object-based scripting language used for creating interactive websites, supported by all major web browsers. It features a syntax similar to C, is case-sensitive, and allows for both internal and external script declarations. JavaScript supports various data types, operators, and enables code reusability through functions and external files.

Uploaded by

py.hariprasad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

123 JavaScript is an object-based scripting language which is lightweight and cross-platform.

JavaScript is not a compiled language, but it is a translated language. The JavaScript Translator
(embedded in the browser) is responsible for translating the JavaScript code for the web
browsers.
Features of JavaScript:
1. All popular web browsers support JavaScript as they provide built-in execution
environments.
2. JavaScript follows the syntax and structure of the C programming language. Thus, it is a
structured programming language.
3. It is a light-weighted and interpreted language.
4. It is a case-sensitive language.
5. JavaScript is cross platform..ie.., supportable in several operating systems and browsers.
Like Windows, macOS, IOS, android, and all browsers like chrome, mozilla....etc.

JavaScript is used to create interactive websites. mainly used for:


Client-side validation, Dynamic drop-down menus, Displaying date and time,
Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box and prompt
dialog box),
Displaying clocks etc.
Java script declaration can be done in 2 ways:
Internal declaration, that is inside the html file
* In body tag
* In head tag
External declaration
In external file, that is outside html file

In html page declaration, whether in body or head tag, must use <script> element.
external script, provides code re-usability. Ie..,single JavaScript file can be used in several html pages.
An external JavaScript file must be saved by .js extension.
It is recommended to embed all JavaScript files into a single file. It increases the speed of the
webpage.

To increase code re-usability we can define functions in script.


Syntax:
function f-name()
{
Executable statement;
}

---------------------------------
<html>
<body>
<h2>Welcome to JavaScript</h2>
<script>
document.write("Welcome Javascript"); // output statement
</script>
</body>
</html>
-------------------------------------

1. In body tag
<html>
<body>
<script>
function msg(){
alert("Hello Javatscript");
document.write(“sample program”);
}
</script>
<form>
<input type="button" value="click" onclick="msg()"/> // calling function through onclick event
</form>
</body>
</html>
------------------------------------------
2. in head tag

<html>
<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. external
function msg()
{
alert("Hello Javascript");
}

<html>
<head>
<script type="text/javascript" src="msgg.js"></script>
</head>
<body>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>

------------------------------------------------
comments:
// single line comments
/* multiline comment*/
------------------------------------------
variable:
must start with datatype ie.., var.
variable name must start with alphabets/_/$ only...digits are not acceptable as a first letter of variable
name, digits can use after first letter.
variable name is case sensitive, x and X are 2 different variables.
2 types: local and global
local: with in method/function or conditional/ control statements
global: declare at script starting and can use in multiple methods/ conditional/control statements.

<html>
<body>
<p> variable declaration in java script</p>
<script>
var x=10;
var X=20;
var _x=30;
var $x=40;
var sum=x+X+_x+$x
document.write(sum);
</script>
</body></html>
local variable
<html>
<body>
<script>
function a(){
var data=200;
document.writeln(data);
}
function b(){
var data=300;
document.writeln(data);
}
a();//calling JavaScript function
b();

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

global variable

<html>
<body>
<script>
var data=200;//global variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data+data);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
--------------------------------------------------------------------
accessing local variable as a global variable through window keyword.
Syn: window.variable-name;
ex: window.a=100;

<html>
<body>
<script>

function a(){
var data=200;
window.value=100;
document.writeln(data);
}
function b(){
var data=300;
document.writeln(data);
document.writeln(window.value);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
------------------------------------
accessing any global variable with in any function
<html>
<body>
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
alert(window.data);
}
function b(){
document.writeln(data+data);
alert(window.data+window.data);
}
a();//calling JavaScript function
b();

</script>
</body>
</html>
---------------------------------
Data types:
are two types of data types in JavaScript.
Primitive data type: which are defined as default in programing by software developers
string, number, boolean, undefined, null
all these 5 types declares with datatype var
var a=10; // number int a=10
var a="value"; // string
var a= true; // booleian
var a; //undefined
var a=null; //null vale assiging
Non-primitive (reference) data type:: which are developed by designer at run time.
object, array..etc
------------------------------------
operators:
Arithmetic Operators. +,-.%,*, ++,--
Comparison (Relational) Operators: ==, >=, >,<,<=..etc
Bitwise Operators: &, |, ^,~, >>, <<
Logical Operators: &&, ||, !=
Assignment Operators" =, *=, -=, %=

You might also like