Javascript
Javascript
Create a folder naming javascript and create a file index.html ,and hold shift!tab key to get html
code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Welcome to Javascript</title>
</head>
<body>
<h1>welcome to the world of javascript and have fun</h1>
<script>
alert("hello");
</script>
</body>
</html>
It is good practice to write js code in separate so create app.js file and write alert here and link the
code file in html file as shown below.
Output-
Right click on browser inspect and click on console we can do below operations as well.
Commenting
Now write the code in app.js file to get output in console as shown below
VARIABLES
We can declare variable using var ,let and const. var is not used in modern javascript
o/p
Here the output will be Gaurav since name is reassigned to Gaurav from mani.
>For below the output will be undefined since var greeting is not assigned any value but this can be
used when conditions are applied.
But we can change object value inside the const var as shown below.
DATA TYPES
Primitive data types – Stored directly in the location of the variable access .Stored in the
stack. Example – String, number, boolean ,null , undefined ,symbol.
//String
const name = "Gowthammi"
console.log(typeof name);
//number
const age= 10;
console.log(typeof age);
//string
const height= "10"
console.log(typeof height);
//boolean
const hassweets= true;
console.log(typeof hassweets);
//number
output
//null
Undefined
//undefined
let a;//if we dont initialize it witha value ,than it is undefined ,by
default variables are undefined
console.log(typeof a);
Symbol
//symbol
//object literals
const person= {
name:" gowthami",
age:25,
};
console.log(typeof person);
output- object
//DATES
output – Object
>
const today = new Date();
console.log(today);
Type conversion and cohesion
//number to a string
let val;
val= String(5+5);
console.log(val);
console.log(typeof val);
console.log(val.length);
output>>
10
String
//boolean to string
let val;
val= String(true);
console.log(val);
console.log(typeof val);
console.log(val.length);
output
true
string
4
//Date to string
//Array to string
let val;
val= String([1,2,3,4]);
console.log(val);
console.log(typeof val);
console.log(val.length);
val=(5).toString();
console.log(val);
console.log(typeof val);
console.log(val.length);
Note – Length is applied only for string
//String to number
let val;
val = Number("5");
console.log(val);
console.log(typeof val);
console.log(val.toFixed(2));
o/p
//Boolean value
let val;
val =Number(true);
console.log(val);
console.log(typeof val);
console.log(val.toFixed(2));
>
let val;
val =Number(false);
console.log(val);
console.log(typeof val);
console.log(val.toFixed(2));
let val;
val =Number(null);
console.log(val);
console.log(typeof val);
console.log(val.toFixed(2));
o/p
//Using parseint()
let val;
val= parseInt("5");
console.log(val);
console.log(typeof val);
console.log(val.toFixed(3));
o/p
//Type cohesion
Here javascript will itself convert and performs action. In the below
val1 which is string added to val2 which will also be converted as
string and gives o/p
let val;
//Escaping
// val = "That's awesome and i cant wait";
//Length
// val= firstName.length;
//concat()
// val= firstName.concat("",lastName);
//Change case
// val = firstName.toUpperCase();
// val= firstName.toLowerCase();
// val= firstName[2];
//Indexof()
// val= firstName.indexOf("a");
// val = firstName.lastIndexOf("a");
//charAt()
// val= firstName.charAt("2");
//Substring
// val=firstName.substring(0,4);
// slice()
// val= firstName.slice(0,4);
//split
// val= str.split(" ");
// val=tags.split(",");
//replace
// val= str.replace("Mani","Gaurav");
//Includes
// val= str.includes("Mani");
console.log(val);
TEMPLATE LITERALS
let html;
// html="<ul><li>Name:" +
// name +
// "</li><li>Age:" +
// age+
// "</li><li>job:" +
// job +
// "</li><li>" +
// city +
// "</li></ul>";
// document.body.innerHTML = html;
OBJECT LITERALS
const person ={
firstName: "Mani",
age: 25,
email: "[email protected]",
hobbies: ["eat","sleep","code"],
address :{
city:"Mangalore",
state:"karnataka"
},
//below is method because function inside object
getBirthYear:function(){
return 2020 - this.age;
//this is used to access property of other object
}
};
let val;
val = person;
console.log(val.hobbies[2]);
console.log(val.firstName);
console.log(val.age);
console.log(val.getBirthYear());
O/P
//ARRAY OF OBJECTS
const people = [
{
firstName:"Mani",
age:25,
},
{
firstName:"Gaurav",
age:27,
},
];
console.log(people);
console.log(people[0]);
console.log(people[0].firstName);
console.log(people[1].firstName);
//now using loops to print all the first name of array of objects
const people = [
{
firstName:"Mani",
age:25,
},
{
firstName:"Gaurav",
age:27,
},
{
firstName:"Kamat",
age:27,
},
];
console.log(numbers);
console.log(number2);
console.log(fruits);
console.log(mixed);
o/p
let val;
val=numbers.length;
console.log(numbers);
console.log(val);
val= Array.isArray(numbers);
console.log(numbers);
console.log(val);
//get a single value
val= numbers[0];
console.log(numbers);
console.log(val);
o/p
//Insert or replace
val= numbers[0];
numbers[2]=100;
console.log(numbers);
console.log(val);
//find the index value
let val;
val= numbers.indexOf(33);
console.log(numbers);
console.log(val);
o/p
// numbers.push(250);
// console.log(numbers);
// numbers.unshift(120);
// console.log(numbers);
// console.log(val);
// numbers.pop();
// console.log(numbers);
// numbers.shift();
// console.log(numbers);
//splicing
// numbers.splice(1,2);
// console.log(numbers);
// numbers.reverse();
// console.log(numbers);
//concat the array
// val=numbers.concat(number2);
// console.log(numbers);
// console.log(val);
//sort
// val= numbers.sort();
// console.log(numbers);
// console.log(val);
// val= numbers.sort(function(x,y) {
// return x-y;
// });
// console.log(numbers);
// console.log(val);
// if(something){
// do something;
// }else{
// do something else
// }
//equal to
// if (id==100) {
// console.log("yes number is 100");
// }else {
// console.log("sorry not 100");
// }
//not equalto
// if(id !=100){
// console.log("true not 100");
// }else {
// console.log("yes its 100");
// }
//Equal to value and type we use trible equal to (===) to check value
and type of the value
//Logical operator
TERNARY OPERATOR
o/p
yes,correct
//Loops in java
o/p
mani
Gaurav
Rakesh
Microdegree
o/p
mani
Gaurav
Rakesh
Microdegree
While Loop
let i =0 ;
while(i < 10) {
console.log(i);
i++;
}
o/p
// Do while
let i = 10;
do {
i++;
console.log(i);
}while(i<10);
o/p
11
//For in loop
Here x holds the key name that is firstname ,age so in order to get
value we have to put person[x]
o/p
Mani
25
o/p
firstName
age
function greet() {
console.log("hie") ;
}
greet();
greet();
o/p
hie
hie
so here we call the function by name and get o/p as many times we need
function greet() {
return 1995
}
console.log(greet());
o/p
1995
>>
o/p
Hello mani
>>
Hello Gaurav
>>
o/p
Hello Mani
Here mani will be preceeding and given priority than Gaurav assigned
Function expression
o/p
hello
>>
o/p
>>
o/p
Gow ran…
const person ={
getBirthday : function (day) {
return `june ${day}`;
},
};
console.log(person.getBirthday(2));
o/p
June 2
//Switch
o/p
>>
switch(dayofweek){
case 0 :
console.log('sunday')
break;
case 1:
console.log('monday')
break;
case 2:
console.log('Tuesday')
break;
case 3:
console.log('wed')
break;
case 4:
console.log('Thu')
break;
case 5:
console.log('Fri')
break;
case 6:
console.log('sat')
break;
}
o/p
wed
Local variable can be accessed only inside the block declared where
global variable can be accessed from anywhere .Local variable is local
to the current block and cant be accessed outside the block. Local
variable has has high precedence than global variable.
//Accessing locally
{
const message ="Hi..;"
console.log(message);
}
o/p
Hi..
//Accessing Globally
{
const message ="Hi..;"
}
console.log(message);
//function
function abc(){
const message = "hi";
console.log(message);
}
abc();
o/p
hi
function abc(){
const message = "hi";
}
console.log(message);
abc();
o/p
//Using if
function abc(){
const message = "hi";
if (true) {
const another ="bye";
console.log(another);
}
}
abc();
o/p
bye
function abc(){
const message = "hi";
if (true) {
const another ="bye";
}
console.log(another);
}
abc();
o/p
function abc(){
const message = "hi";
if (true) {
const another ="bye";
}
for (let i=0 ;i<5; i++){
console.log(i);
}
}
abc();
o/p
function abc(){
const message = "hi";
if (true) {
const another ="bye";
}
for (let i=0 ;i<5; i++){
}
console.log(i);
}
abc();
o/p
//Multiple functions
function abc(){
const message = "hi";
console.log(message);
}
abc();
function pqr(){
const message = "bie";
console.log(message);
}
pqr();
o/p
hi
bie
//Global scope
const color="blue";
function abc(){
const message = "hi";
console.log(message);
console.log(color);
}
abc();
o/p
hi
blue
Precedence of local and global variable
const color="blue";
function abc(){
const message = "hi";
const color="red";
console.log(color);
}
abc();
o/p
red
>Using let
function start() {
for (let i=0; i<5;i++){
console.log(i);
}
}
start();
o/p
0 1 2 3 4
function start() {
for (let i=0; i<5;i++){
}
console.log(i);
}
start();
o/p
unreference error
>using var
function start() {
for ( var i=0; i<5;i++){
console.log(i);
}
}
start();
o/p
0 1 2 3 4
function start() {
for ( var i=0; i<5;i++){
}
console.log(i);
}
start();
o/p
Note :
function start() {
for ( var i=0; i<5;i++){
if(true) {
let color = "blue"; //if we use var,const in let space also
same ouput
console.log(color);
}
}
}
start();
o/p
5 blue
function start() {
for ( var i=0; i<5;i++){
if(true) {
let color = "blue";/if we const also same o/p
}
console.log(color);
}
}
start();
o/p
uncaught reference error
function start() {
for ( var i=0; i<5;i++){
if(true) {
var color = "blue";
}
console.log(color);
}
}
start();
o/p
5 blue
Another ex:
var Attr=25;
o/p
Hence we are not using the var much as it affects the application.
DOM
Once we write Html it will create the Dom object that Document object
module as shown below which can be seen in window property.