00 BT JS Basic

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

// single line comments.

/*
multi line comments.
*/
<!-HTML comments.->

===========================================================================
single statements do not need ";" but multiple statements need ";"
e.g. dcoument.write("hello");

===========================================================================
Variable conventions
the name can start with _ and alphabet but not with number.
variables are case sensitive.
reserved keywords cannot be used.

===========================================================================
NaN: not a number.
var x = 1; var y = "A";
dcoument.write(x/y); //it will result in NaN.

===========================================================================
Compount Assignment Operator
var z = 1; var b = 2;
var a = b +=y; //now a will be 3.

===========================================================================
Comparison operator
x === y //this cares for type.
x !== y //this cares for type.
x == y //this does not care for type.

===========================================================================
alert, confirm and prompt
alert("Click to proceed"); //we just get an OK option.
confirm("do you want to proceed"); //here we get cancel and ok options.

var s = prompt("Enter the id");


document.write(s);

===========================================================================
switch(n){
case "arnav":
document.write("one");
break;
case "kanav":
document.write("two");
break;
default:
document.write("default.")
}
===========================================================================
break: loops break and code execution moves to next code block.
continue: execution just skip current iteration.

===========================================================================
Functions:
Reuse, call /event etc.
we define the function and then use it.

===========================================================================
Closures are functions that we define inside another function. These functions have
access to the parent functions variables as well as the global variables.

function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert(name); // use variable declared in the parent function
}
displayName();
}

init();

===========================================================================
Arrays

<script>
var a = new Array(2, 4.5, "Arnav");
document.write(a + "<br/>");
document.write(a.length + "<br/>");
document.write(a[0] + " " + a[1] + " " + a[2] + "<br/>");

var b = [2, 4.3, "Kanav"];


document.write("Array b:" + b + "<br/>");

var c = new Array();


c[0]=2;
c[1]=3.23;
c[2]="Priyanka";

a = a.concat(b,19,20,c);
document.write(a + "<br/>");
document.write(a.reverse() + "<br/>");
document.write(a.join("-");

</script>
===========================================================================
setInterval

function continuousDisplay(){
setInterval(displayDate, 1000);
}

===========================================================================
Error Handling (In JS we use Error instead of Exception). try-catch-finally
Syntax Error e.g. alert("hello

RuntimError:

try{
doSomething();
}catch(obj){
console.log(obj.message);
}
--------------------------------
Throwing custom Errors: throw "Enter error text here"

function validateAge(){
try{
var age = document.getElementById("age").value;
if(age=""){
throw "Age cannot be empty";
}
} catch(obj){
document.getElementById("errorinfo").innerHTML = obj;
} finally{
document.getElementById("finallyinfo").innerHTML= "closing resources";
}
}

===========================================================================
DOM (Document Object Model)
The Document is the HTML Document that is loaded into memory and the entire HTML
documentis represented as objects e.g. html, body, p etc.

Access HTML e.g.


function handleHTML((){
document.getElementById("para1").align = "center";
document.getElementById("para1").style.color = "green";
document.getElementById("img1").src="JS.jpg";
}

---------------------------------------------------------------
BOM (Browser Object Model)
The Browser Object Model gives us access to the different functionality that is
available in the web browser in the form of objects. The browser itself is
represented as the window object in JavaScript. And you have already been using
different functions on the window object such as alert, confirm and prompt for
popups, setTimeout, setInterval, location etc.

The Browser Object Model is not part of the JavaScript standard and it is up to the
web browser to provide
support for it. And these names can be different in different web browsers, but
most of the browsers support the same naming convention.

Navigator:
<script>
document.write("App name " + navigator.appName + "<br/>");
document.write("App Code Name " + navigator.appCodeName +"<br/>");
document.write("App Code Name " + navigator.javaEnabled+ "<br/>");

</script>
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================
===========================================================================

You might also like