JAVASCRIPT
JAVASCRIPT
JAVASCRIPT 1
VARIABLES
//1. decleration
let x;
//2. assignment
x=200;
//printing
console.log(x);
let x=200:
console.log(x);
${} //placeholder
Datatypes
JAVASCRIPT 2
//NUMBERS
let age=25;
let price=10.99;
let gpa=2.3;
console.log(typeof gpa);
console.log(typeof price);
console.log(typeof age);
console.log(`You are ${age} years old.`);
console.log(`The price is $${price}.`);
console.log(`Your gpa is ${gpa}.`);
//OUTPUT
number
number
number
You are 25 years old.
The price is $10.99.
Your gpa is 2.3.
//STRINGS
console.log(typeof name);
console.log(typeof favouritefood);
console.log(typeof email);
//OUTPUT
string
JAVASCRIPT 3
string
string
My name is Bro.
I love Pizza.
My email is [email protected]
//BOOLEAN
let online=true;
let forSale=false;
let isStudent=true;
console.log(typeof online);
console.log(`is he online?: ${online}`);
console.log(`is this car fo sale? ${forSale}`);
console.log(`Is this student enrolled? ${isStudent}`);
//OUTPUT
boolean
is he online?: true
is this car fo sale? false
Is this student enrolled? true
EXCSERCISE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, init
ial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p id="myName">What is your name?</p>
JAVASCRIPT 4
<p id="myAge">What is you age?</p>
<p id="status">Are you a student?</p>
<script>
let fullName = "Rob";
let age = 23;
let isStudent = true;
document.getElementById("myName").textContent = `My n
ame is ${fullName}`; //getElementById is used to fetch the
element with a particular id.
document.getElementById("myAge").textContent=`I am
${age} years old.`; //textContent is used to change the tex
t
document.getElementById("status").textContent = isStu
dent;
</script>
</body>
</html>
KEYWORDS
Their meaning and purpose is already defined within the Javascript.
JAVASCRIPT 5
let x=30; //Error shows
JAVASCRIPT 6
for: A loop with a counter, usually used when the number of
iterations is known.
for (let i = 0; i < 5; i++) {
console.log(i);
}
//OUTPUT
0
1
2
3
4
while: A loop continues to run as long as the condition is
true.
let i=0;
while(i<5){
console.log(i);
i++;
}
//OUTPUT
0
1
2
3
4
do...while: Similar to while loop. But it guarantees at lea
st one iteration, as the condition is checked after the loo
p body.
let i = 0;
do{
console.log(i);
i++;
} while (i<5);
//OUTPUT
0
1
2
3
JAVASCRIPT 7
4
4. function: functions can be called using their naame and
passing any required arguments.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("insha"));
//OUTPUT
Hello, insha!
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet(123));
//OUTPUT
Hello, 123!
function add(a, b) {
return a + b;
}
let sum = add(5, 3); // sum is 8
5. switch:
let day = 3;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
JAVASCRIPT 8
break;
default:
console.log('Other day');
}
//OUTPUT
Wednesday
let day = 2;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
break;
default:
console.log('Other day');
}
//OUTPUT
Tuesday
let day = 5;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
break;
default:
JAVASCRIPT 9
console.log('Other day');
}
//OUTPUT
Other day
try {
// code that may throw an error
let result = riskyOperation();
} catch (error) {
console.error(error); // handle the error
} finally {
console.log('This runs no matter what');
}
8. class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I a
m ${this.age} years old.`);
}
}
JAVASCRIPT 10
//Output
Hello, my name is Alice and I am 30 years old.
class Car {
constructor(brand) {
this.brand = brand;
}
display() {
console.log(`This car is a ${this.brand}`);
}
}
10. new
let a = null;
let b;
console.log(b); // undefined
JAVASCRIPT 11
//OUTPUT
You don't need an umbrella.
<script>src="index.js"</script>
in mathUtil.js:
in index.js:
import{PI, getCircumference, getArea, getVolume} fr
om './mathUtil.js';
JAVASCRIPT 12
console.log(PI);
const circumference = getCircumference(10);
const area = getArea(10);
const volume = getVolume(10);
console.log(`${circumference.tofixed(2)}cm`);
console.log(`${area.tofixed(2)}cm^2`);
console.log(`${volume.tofixed(2)}cm^3`);
//output
3 .14159
62.83cm
314.16cm^2
1256.64cm^3
OPERATORS
Comparison Operators
== equal to
!= not equal to
=== strict equal to
!== strict not equal to
Equal to
var a=5;
var b= 5;
var c= "Monica"
console.log(a==5); //true
console.log(b=='5'); //true
console.log(c=="monica") //false due to case sensitivity of
string
Not equal to
JAVASCRIPT 13
var a = 4;
var b = "Monika";
console.log(a!=3); //true
console.log(b!="Monika"); //false
Strict equal to
var a=10;
console.log(a===10); //true
console.log(a==='10'); //false .... because strict equal to
check the datatype also
a=2;
b="Monica"
Greater than
a=20;
b=10;
console.log(a>b); //true
less than
a=10;
b=20;
JAVASCRIPT 14
console.log(a<b); //true
a=2;
console.log(a<=3); //true
console.log(a<=2); //true
a=2;
console.log(a>=3); //false
console.log(a>=2); //true
console.log(a>=1); //true
CONDITIONAL OPERATORS
Ternary operators
Short form of if else condition.
//Output : Yes
Logical Operators
AND : true if both the conditions are true.
JAVASCRIPT 15
= assigning value to an operator
Undefined vs Null
undefined : A variable has been declared but not yet been assigned.
let x;
console.log(x); //logs undefined
let y = null;
cinsole.log(y); //logs 'null'
variables declared inside the curly braces are called block scope.
let and const are also block scope. since the variables are used within a
particular block. let used to declare a variable which can be changed as
program progress, while const is a variable that can be declared inside the
program which cannot be changed throughout the program.
JAVASCRIPT 16
function hello() // calls a function
{
if (true)
{
var a = 'Javascript'; //function scoped
let b = 'c++'; //block scoped
const c = 'Python'; //block scoped
console. log(a) ; //logs Javascript
console. log(b) ; //logs c++
console. log(c) ; //logs Python
}
console. log("outside If statement") //logs outside If
statement
console. log(a) ; //logs Javascript since var is a func
tion blocked element.
console. log(b) ; //logs reference error since if block
has ended
console. log(c); //logs reference error since if block
has ended.
}
hello() ;
function func() {
let a = 'Geeks';
if (true) {
let a = 'GeeksforGeeks'; // New value assigned
console.log(a);
JAVASCRIPT 17
}
console.log(a);
}
func();
//outpt
//GeeksforGeeks
//Geeks
illegal shadowing: declaring var in the outside block and then declaring new
variable in the inside block using let is legal
function func() {
var a = 'Geeks';
let b = 'Geeks';
if (true) {
let a = 'GeeksforGeeks'; // Legal Shadowing
var b = 'Geeks'; // Illegal Shadowing
console.log(a); // It will print 'GeeksforGeeks'
console.log(b); // It will print error
}
}
func();
let a = 10;
let b = a;
a = 20;
JAVASCRIPT 18
console.log(a); //logs 20
console.log(b); //logs 10
then var b declared and assigned variable a; which means the memory
space labelled by variable b stores the value of memory space labelled by
variable a, i.e. 10.
But since we passed only the value to a, the change in variable a doesn’t
affect the variable b.
Pass by reference
obj1=20;
console.log(obj1.value); //logs 20
console.log(obj2.value); //logs 20
Since obj2 is pointed towards the value of obj1, the value in obj2 also
changes corresponding to obj1. i.e. 20
Type coercion
This is basically the technique of converting a value from one datatype to
another datatype
code conversion and code coercion is doing the same thing but with one
key difference.
JAVASCRIPT 19
type coercion is implicit ; can be automatically changed to another
datatype.
let a = 10;
let b = '20';
let sum = a + b;
JAVASCRIPT 20