Java Script CS242
Java Script CS242
CS242
Department of CSE
IIT Guwahati
Akshay Parekh
Phd, CSE
JavaScript Facts ●
●
Lightweight programming language.
Developed by Brendan Eich at Netscape in
1995..
● Generally used for client-side scripting,
making web-pages dynamic, responsive, and
interactive.
● Also used for plug-in scripting,
browser-based game scripting, etc.
Installation and
● No installation is required.
● Majority of the web-browser supports JS.
Running ● To run,
○ JS code is written in a html file.
○ Open the file in browser.
○ Inspect Element.
○ Console
<html>
<head>
<script>
console.log(“Hello
World!”);
</script>
</head>
<body>
</body>
</html>
Statement & Comment Every Statement in JS is like,
console.log(“Hello World!”);
Semicolon
marks the
end
var x;
x = 5;
console.log(x);
var y = x + 5;
console.log(x);
console.log(typeof x);
Primitive Data Types
● Number: Integer and Real.
var x = 25;
● String: immutable sequence of characters.
var robin;
● Null: represents explicitly empty value.
Operators
var x = 25 + 4;
var y;
y = x - 20;
console.log(x, y);
Expressions involving string,
alert(“I am Ironman”);
prompt(“Input Username”);
● Boolean(value)
● parseInt(value)
● parseFloat(value)
● Number(value)
● String(value)
Functions function keyword is used while defining function.
function getName(){
var name = prompt(‘Enter Name’)
console.log(name)
}
getName()
function getDetail(str){
value = prompt(‘Enter ’+str)
console.log(value)
}
getDetail(‘roll’)
function getDetail(str){
value = Number(prompt(‘Enter
’+str))
return value
}
var roll = getDetail(‘roll’)
console.log(roll, typeof roll)
Conditional ● If-else are similar to other programming
languages like C/C++/JAVA.
Statement if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
Loops ● for
● while
while (condition) {
statements;
}
● do while
do {
statements;
} while (condition);
Strings length property gives then size (number of
character) of the string.
var sup = ‘I am Ironman;
console.log(sup);
console(sup.length);
String Indexing,
console.log(sup[0]);
console.log(sup[sup.length-1]);
console.log(stud.length)
Array indexing
console.log(stud[0]);
console.log(stud[stud.length-1]);
Array Explore following array methods,
var aboutMe = {
degree: ‘PhD’,
department: ‘CSE’,
University: ‘IIT G’
};
var myDegree = aboutMe.degree
console.log(myDegree)
var myDegree = aboutMe[‘degree’]
console.log(myDegree)
var aboutMe = {
degree: ‘PhD’,
department: ‘CSE’,
University: ‘IIT G’,
name : function() {
name = prompt(‘Input Name: ’);
return name;
}
};
console.log(aboutMe)
console.log(aboutMe.name)
Object.keys(aboutme)