Notes
Notes
Notes
list=PLzdWZT-ZJD0_IqetEOq2FxjQucklQi9KG
JAVASCRIPT SYNTAX:
<html>
<head>
<title> Javascript syntax </title>
<script type="text/javascript">
document.write("Hi this is sample program");
</script>
</head>
<body>
</body>
</html>
3.<p id="demo"></p>
<script type="text/javascript">
document.getElementById("demo").innerHTML="This is for Form Validation";
</script>
FUNCTIONS
A function is a set of statements enclosed within curly brackets ({}) that take
inputs, do the computation, and provide the resultant output.
You can call a function multiple times, thereby allowing reusability and modularity
in C programming.
<script>
function _name(){
document.write("Welcome to function");
}
_name(); //function calling... without calling the function will not execute.
</script>
return type function-- a return statement ends the execution of a function and
returns the control to the calling function.
<script>
function multiply(number){
return number*number*number;
document.write("hello"); //it does not execute because the return type
function.
}
</Script>
<script>
document.write(multiply(4));
</script>
<script>
var a=20;
var b=30;
document.write(a+b);
</script>
<script>
var x=45; //Global variable
function add(){
document.write(x);
}
add();
document.write(x+20);
</script>
DATATYPE
Data types are declarations for variables. This determines the type and size of
data associated with variables.
There are 5 types
1) Number Datatype(int,float)
2) String Datatype
3) Boolean Datatype
4) Regular Expression
5) Array
1) Number Datatype
<script>
var x=35;
document.write(x);
</script>
<script>
var x=35;
console.log(x)
</script>
2) String Datatype
<script>
var x="Rama Krishna";
document.write("My Name is:" + x);
</script>
3) Boolean Datatype
<script>
var x=true;
document.write(x);
</script>
4)Regular Expressions
<script>
var x=25;
var y=30;
var z=x+y;
document.write(z);
</script>
5) Array
<script>
const cars = ["Saab", "Volvo", "BMW"];
document.write(cars);
</script>
Operator
There are 6 types of operators
1) Arithmetic Operators (+, -, *, /, %, ++, --) [%(modulas-Remainder)]
2) Assignment Operators (=, +=, -=, *=, /=, %=)
3) Comparison/Relational Operators (==, !=, <, <=, >, >=) [To check one
condition only]
4) Bitwise Operators (&, |, ^, <<, >>, >>>) [To check Multiple Conditions]
[^(xor)(anyone is true or any one is false)]
5) Logical Operators(&&, ||, !)
6) Terinory Operator-------------a=10, b=20 big = a>b?a*5:b*2
<script>
var fname="Rama";
var sname="krishna";
if(fname=="Rama" | sname=="gopal"){
document.write("Welcome" +);
}
</script>
IF-Else Statement
<script>
var x=20;
if(x%2 == 0){
document.write("X is even number");
}
else{
document.write("X is odd number");
}
Ex:
<script>
var marks=85;
if(marks>90){
document.write("A Grade");
}else if(marks>75){
document.write("B Grade");
}else if(marks>60){
document.write("C Grade");
}else if(marks>45){
document.write("D Grade");
}else{
document.write("FAIL");
}
</script>
Loops
There are three types of loops for,while,do-while
<script>// for loop is used when no of iterations we know
for(initialization; condition; increment){
//code will be executed }
</script>
EX:
<script>
for(i=0; i<=5; i++){
document.write("Hello <br/>");
}
</script>
Ex:
<script>
for(i=1; i<=10; i++){
document.write(2*i +"<br/>");
//document.write("2 x" + i + "=" + 2*i +"<br/>");
}
</script>
Ex:
<script>
var i=1;
while(i<=50){ //while loop is used when no of iterations we din't know
document.write(i + "<br/>");
i=i+2;
}
</script>
Ex:
<script>
var i=1;
do{
document.write(i + "<br/>");
i=i+2;
}
while(i<=50)
</script>
SWITCH STATEMENT
<script>
var marks=60;
switch(true){
case marks>90 :result="A Grade";
break;
case marks>75 :result="B Grade";
break;
case marks>60 :result="C Grade";
break;
case marks>45 :result="D Grade";
break;
default: result="FAIL";
}
document.write(result);
</script>
ARRAYS
Arrays are declared in three ways
1) By array literal
2) Array Directly(New Keyword)
3) Array Constructor
Object
<script>
var student1={rollno:6, name:"Hari", Branch:"ECE"}
document.write(student1.rollno + "" + student1.name + "" +student1.Branch);
</script>
Window Object:
<script>
window.alert("Hello");
</script>
<script>
window.confirm("Hello");
</script>
<script>
window.prompt("How are you?");
</script>
<script>
var x = window.prompt("How are you?");
document.write(x);
</script>
<script>
window.open("http://www.google.com");
</script>
<script>
function abc(){
window.open("http://www.google.com");
}
</script>
<input type="button" value="google" onclick="abc()">
<script>
window.close();
</script>
<script>
var w=window.innerWidth;
var h=window.innerHeight;
document.write(w+ " " + h);
</script>
<script>
function myFunction(){
alert("Hello");
}
setInterval(myFunction, 2000);
</script>
<script>
function myFunction(){
alert("Your time is over");
}
setTimeout(myFunction, 5000);
</script>
History Object:
<a href="second.html"> Second Page</a>
<script>
function goBack(){
history.back();
}
</script>
<input type="button" value="Back" onclick="goBack();">
Navigator object:
<script>
document.write(navigator.appName);
</script>
<script>
document.write(navigator.appVersion);
</script>
<script>
document.write(navigator.cookieEnabled);
</script>