JavaScript I
JavaScript I
• Introduction to JavaScript
• Need of JavaScript
• Example
• JavaScript and Java
• Comments
• Variables
• Identifiers
• Operators
Introduction to JavaScript
What is JavaScript?
Object-based Structured
scripting language JavaScript
programming
Translator
Use of prototypes language
• Decision Making
• Submitting Forms
• Performing complex mathematical functions
• Automatic online data validation
Need of JavaScript(contd.)
Read elements
from
Perform actions documents
based on
Manipulate text
conditions
Create pop-up
Need of Perform
windows JavaScript mathematical
calculations
Retrieve the
Client side
current date
validation
and time
Example
<script>
document.write(“Example of JavaScript");
</script>
Details Script tag: Specifies JavaScript
Text/javaScript: Information to
browser about data
JavaScript can
be placed in Between the Between the
external files body tag of html head tag of html
(.js)
JavaScript and Java
JavaScript Java
Interpreted in source code on Complied in bytecode
browser
Integrate into HTML program for Must be compiled before
execution execution
• Uses
To explain JavaScript code
To prevent execution for testing alternative
code
• Types
Single line Comments
Multi-line Comments
Single Line Comments
• Starts with //
• Most commonly used
• Example:
• // Single line comment
• Used at the end of line to explain the code
Multiple Line/ Block Comments
Case sensitive
JavaScript Variables (Contd.)
Variable Declaration
1. var: Used in all older version of JavaScript.
var x;
x = 5;
2. let: Used in newer version of JavaScript, if value of variable
can change
let y;
y = 3;
3. const: Value must be assigned in declaration
const x = 4;
4. Undefined: No value is given
let x;
JavaScript Variables (Contd.)
<script>
function xyz() {
var x=11; //local variable
}
</script>
JavaScript Variables(contd.)
<script>
var value=50; //global variable
function a(){
alert(value);
}
function b(){
alert(value);
}
</script>
JavaScript Variables(contd.)
<p id=“example"></p>
<script>
let stdName = “abc";
document.getElementById("example").innerHT
ML = stdName;
</script>
Output: abc
JavaScript Datatypes
Datatypes
Primitive
String, Number, Boolean,
Undefined, Null
Datatype Description
Object-based language
Object literal
Syntax object={property1:value1,property2:value2.....propertyN:valueN}
<script>
std={id:11,name:"Kumar"}
Example document.write(std.id+" "+std.name);
</script>
Output: 11 Kumar
Instance of object directly
Syntax
• var objectname=new Object();
Example
<script>
var std=new Object();
std.id=21;
std.name=“abc";
document.write(std.id+" "+std.name);
</script>
Output: 21 abc
Use of object constructor
Create function with Each argument value can be assigned in
arguments current object using this keyword
<script>
function std(id,name){
this.id=id;
this.name=name; Output: 11 abc
}
s=new std(11 ,“abc");
document.write(s.id+" "+s.name);
</script>
Primitive Datatypes
String
Ways to create
string
• slice(start, end)
• Example:
let str = “NIT Patna Bihar”
let result_1 = str.slice(4,9);
Output: Patna
let result_2 = str.slice(-1,-6);
Output: Bihar
• substr(start, length)
let result_3 = str.substr(4,5);
Output: Patna
Replacing string
replace()
• Replaces a specified value with another value
• Example:
let str = “abc";
let result = str.replace(“abc", “def");
Output: def
Let char = str.charAt(0);
Output: a
Converting to upper and lowercase
toUpperCase():
toLowerCase():
• toUpperCase():
• Example
let str = “Keep It Up”;
let str1 = str. toUpperCase();
Result: KEEP IT UP
• toLowerCase():
• Example
let str2 = str. toLowerCase();
Result: keep it up
String Search Methods
• String indexOf()
• Example:
let txt = ”abc def ghi def";
txt.indexOf("def");
Output: 4
• String lastIndexOf()
• Example:
• txt. lastIndexOf(def);
• Output: 12
String startsWith()
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const arr = ["ab", "cd", "ef"];
document.getElementById("demo").innerHTML = arr;
</script>
</body>
</html>
Array object
Object literal
Syntax Example
• var arrayname=[value1,val <script>
ue2.....valueN]; var std=*“ab",“cd",“ef"+;
for (i=0;i<std.length;i++){
document.write(std[i] + "<br/>");
}
</script>
Use of Array Object directly (new keyword)
Example
<script>
Syntax var i;
var emp = new Array();
• var arrayname=new Array(); emp[0] = "A";
emp[1] = “B";
emp[2] = “C";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
Use of array constructor
Example
<script>
Basics var emp=new Array(“A",“B",“C");
for (i=0;i<emp.length;i++){
Passing arguments in document.write(emp[i] + "<br>");
constructor }
</script>
Array Methods
Splice() slice()
splice(): Adds or removes Slice out a piece of an array
new element at specified into a new array
position
reverse() sort()
Types
Bitwise Assignment
operator operators
Comparison String
operators operators
Operators (contd.)
• Performs arithmetic on
Arithmetic number (literals or
operators variables)
• +, -, *,/, %, ++, --,**
= a=1 a=1 -
+= x+=y x=x+y 15
-= x-=y x=x-y 5
*= x*=y x=x*y 50
/= x/=y x=x/y 2
%= x%=y x=x%y 0
Operators (contd.)
• + operator/Concatenation operator: To
String concatenate strings
operators • +=: can also be used
• Adding number and string: return string
• Logic between
Logical operators variables and values
• &&(and), II (or), !(not)
Operators (contd.)
Logical Operators
let x = 1;
let y = 2;
! NOT !(x>y),
returns true
Operators (contd.)
Bitwise Operators
Operator Name
& AND
| OR
^ Bitwise XOR
~ Bitwise NOT
<< Bitwise Left Shift
>> Bitwise Left Shift