Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Java Script CS242

This document provides an introduction to JavaScript, covering topics such as: 1. JavaScript is a lightweight programming language used primarily for client-side scripting to make web pages dynamic. 2. It discusses JavaScript variables, data types, expressions, operators, conditional statements, functions, arrays and objects. 3. The document provides examples and references for further reading on JavaScript statements, comments, variables, primitive data types, expressions, operators, pop-up boxes, functions, conditional statements, loops, strings, arrays, objects and references.

Uploaded by

Preeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Java Script CS242

This document provides an introduction to JavaScript, covering topics such as: 1. JavaScript is a lightweight programming language used primarily for client-side scripting to make web pages dynamic. 2. It discusses JavaScript variables, data types, expressions, operators, conditional statements, functions, arrays and objects. 3. The document provides examples and references for further reading on JavaScript statements, comments, variables, primitive data types, expressions, operators, pop-up boxes, functions, conditional statements, loops, strings, arrays, objects and references.

Uploaded by

Preeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Java Script

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

Single line comment,

// This is single line comment

Multi line comment,


/* This is
multi line
Comment */
Variables
Variable are declared with var keyword.

Declaration and Initialization separately,

var x;
x = 5;
console.log(x);

Declaration and initialization simultaneously

var y = x + 5;
console.log(x);

● Variables can later be re-assigned.


● Variables can also store result of an expression.
● Types are not specified (loosely typed). Can find
out variable’s type

console.log(typeof x);
Primitive Data Types
● Number: Integer and Real.

var x = 25;
● String: immutable sequence of characters.

var genre = ‘rock and roll’;


● Boolean: Logical values True or False.

var iambatman = true;


● Undefined: not defined yet

var robin;
● Null: represents explicitly empty value.

var aquamanOnLand = null;


Expressions and
Expressions involving numbers,

Operators
var x = 25 + 4;
var y;
y = x - 20;
console.log(x, y);
Expressions involving string,

var name = ‘Dany';


var greeting = 'Hello ' + name;
console.log(greeting);

‘+’ is also used for string concatenation.

● All the operators (arithmetic, logical, etc) are


similar to C/C++/Java.
● Most operators automatically do
type-conversion.
Pop-up Box 1. To display message box on screen.

alert(“I am Ironman”);

2. To accept Yes/No from user.

confirm(“Are you Sure?”);

3. To accept user input.

prompt(“Input Username”);

Functions for type-conversion,

● 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

for (initialization; condition; update) {


statements;
}

● 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]);

Explore following string methods,

charAt, charCodeAt, fromCharCode,


indexOf, lastIndexOf, replace, split,
substring, toLowerCase, toUpperCase
Arrays An array is a type of data-type that holds an
ordered list of values, of any type.
var arrayName = [ele0, ele1, ...];
var stud = [‘Peter’, 123, ‘Spiderman’]
console.log(stud)

length property gives the size of array

console.log(stud.length)

Array indexing

console.log(stud[0]);
console.log(stud[stud.length-1]);
Array Explore following array methods,

concat, join, pop, push, reverse,


shift, slice, sort, splice, toString,
unshift

Adding/Modifying elements in an array,

stud[4] = ‘Team Ironman’


Objects
● Objects are data types that lets us store
collection of properties and methods.
● Similar to class.

var aboutMe = {
degree: ‘PhD’,
department: ‘CSE’,
University: ‘IIT G’
};
var myDegree = aboutMe.degree
console.log(myDegree)
var myDegree = aboutMe[‘degree’]
console.log(myDegree)

● Non-existing property will return undefined.


● Can change values of existing properties.
● Can add new properties
● Can delete existing property using delete.
Objects: Methods
● Object properties can also be functions. Object
functions are called "methods".

var aboutMe = {
degree: ‘PhD’,
department: ‘CSE’,
University: ‘IIT G’,
name : function() {
name = prompt(‘Input Name: ’);
return name;
}
};
console.log(aboutMe)
console.log(aboutMe.name)

● To list all the properties of an object

Object.keys(aboutme)

● Explore built-in Objects (Date, Math, Array,


String).
References
● https://developer.mozilla.org/en-US/docs/Web/
JavaScript/Guide
● https://www.w3schools.com/js/default.asp

You might also like