Professional Documents
Culture Documents
JavaScript Intro
JavaScript Intro
JavaScript Intro
To
JavaScript
Client Side Scripting
• E.g., var x;
let x, y;
Variable Initialization
• E.g. var x; x = 5;
var x = 5;
Variable Initialization
• String
- series of characters
- written within single or double quotes
- e.g., var name = “John”;
var city = ‘Pune’;
var answer = “It’s raining”;
Datatypes
• Number
- store numeric value
- written with or without decimals
- e.g., var x = 56;
var y = 23.45;
var z = 53e-4;
Datatypes
• Boolean
- can have only two values – true or false
- used in conditional testing
- e.g., var a = 10; var b = 10; var c = 12;
(a == b) – returns true
(a == c) – returns false
Undefined
• Example –
typeof “abc”; // returns string
typeof 21; // returns number
typeof (3 + 5); // returns number
Displaying Output
<body>
<p id = “p1”></p>
<script>
document.getElementById(“p1”).
innerHTML
= “Hello World”;
</script>
Using document.write
<script>
document.write(“Hello World”);
</script>
OR
<input type = “button”
onclick = “ document.write(‘Hello
World’)”
value = “Try It”>
Using window.alert
<script>
console.log(“Just to Check”);
</script>
Operators
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators
• Type Operators
• Bitwise Operators
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus
++ Increment
-- Decrement
Arithmetic Operators
<script>
document.write(2 + 3);
document.write(8 - 5);
document.write(3 * 4);
document.write(7 % 3);
</script>
Assignment Operators
Operator Description
typeof returns the type of a variable
instanceof returns true if an object is an
instance of an object type
Type Operators
<script>
var a = 15;
var b = “xyz”;
var c = a < 10;
document.write(typeof(a));
document.write (typeof(b));
document.write(typeof(c));
</script>
Bitwise Operators
& AND
| OR
~ NOT
^ XOR
()
!, -, ++, --
*, /, %
+, -
<, <=, >, >=
==, !=
&&
||
?:
=, +=, -=,*=, /=, %=, <<=, >>=, >>>=, &=, ^=, |=
Comments