java script day1
java script day1
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.
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.
---------------------------------
<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" =, *=, -=, %=