Javascript
Javascript
What is java-script?
➢ Javascript is a high-level programming language primarily used for creating interactive
and dynamic web pages.
➢ Javascript runs on the client side and executes by the user's web browser.
Javascript libraries in frontend
➢ Html
➢ Css
➢ Javascript
➢ Reactjs
➢ Angularjs
➢ Vue
➢ Next
➢ Typescript
➢ Gatsby
➢ React Native
Javascript libraries in the backend
➢ Expressjs
➢ Nextjs
➢ Gatsbyjs
➢ Nuxtjs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>hello world</h1>
<!-- <script>
alert("hello world"); --internally
</script> -->
<script src="project.js"> //externally
</script>
</body>
</html>
Datatypes in javascript
➢ String
➢ Number
➢ Boolean Primitive datatypes
➢ Null
➢ Undefined
➢ Array
➢ Object Complex datatypes
Operators &Expressions
➢ Arithmetic operators
➢ Assignment operators
➢ Comparison operators
➢ Logical operators
Arithmetic operators
✓ Addition
✓ Subtraction
✓ Multiplication
✓ Division
✓ Remainder(Modulus)
✓ Increment
var a=120;
var b=20;
document.write("result", a+b);
document.write("result", a-b);
document.write("result", a*b);
document.write("result", a/b);
document.write("result", a%b);
a++;
document.write("result", a);
a--;
document.write("result", a);
++a;
document.write("result", a);
--a;
document.write("result", a);
console.log(a+b);
//-----------------
console.log(a++ + ++a); //22
let e=true;//1
let f=false;//0
console.log(e+f); //1
//------------------
console.log(true+true+3*true)//5
console.log("java"+10+20); //java1020
console.log(10+20+"java")//30java
console.log("--------------")
let a=10;
let b=20;
let c=30;
console.log(a<b<c)//true
console.log(a>b>c)//false
console.log(c<b<a)//true
console.log(c>b>a)//false
console.log("--------------")
let i="10";
let j=10;
console.log(i==j);//true //check only values
console.log(i===j);//false //check both values and datatype
console.log(i!=j);//false
console.log(i!==j);//true
console.log("--------------")
let x=2;
console.log(x*x*x);//8
console.log(x**3)//8 //exponent
console.log("--------------")
console.log(typeof(x));
logical operators
✓ logical NOT(!)
✓ logical AND(&&)
✓ logical OR(||)
// -------------------logical operators-------------
var x=100;
var y=50;
var z=200;
if(x==y&&y<z){
document.write("Yes correct answer")
}
else{
document.write("Wrong answer")
}
if(x==y||y<z){
document.write("Yes correct answer")
}
else{
document.write("Wrong answer")
}
if(x!==y&&y<z){
document.write("Yes correct answer")
}
else{
document.write("Wrong answer")}
Conditional statements
✓ If
✓ If -else
✓ If-else-if
✓ switch
//-------------------conditional statements--------------------
//if,if-else,if-elseif-else
var game="Cricket";
let captain ="Dhoni";
const tennsi="sania"
if(game=="Cricket"){
document.write("match captain is ",captain);
}
else{
document.write("Wrong answer")
}
//------------
else if(y>z){
document.write("blue color");
}
else if(z>x){
document.write("yellow color");
}
else{
document.write("All are Wrong Statements");
}
//------------------------------------Dailog Boxes----------------------------------------
alert("This is a alert message")
prompt("this is a prompt message");
confirm("This is a confirmation message");
//----------------------------------------------------------------------------------------
var woman=confirm("Are you a woman");
if(woman){
Type conversion
//---------------Type Conversion--------------------------------------------
var a="55";
var b=44;
document.write(typeof a +"\n");//string
document.write(typeof b);//number
----
var z=parseInt(a);
document.write(typeof z);//number converted from String
var x=String(b);
document.write(typeof x);//String converted from number
Switchcase statements
//------------------------------Switch Expressions-----------------------
switch(true){
case 10>20: myAnswer="red color"; break;
case 20<30: myAnswer="green color"; break;
case 50>20: myAnswer="black color"; break;
case 100==200: myAnswer="blue color"; break;
default: myAnswer="none of these above are correct"; break;
}
document.write(myAnswer);
----------------------------------------------
let Name=prompt("please Enter your name");
let marks=prompt("Enter your marks");
switch(true) {
case marks>90&&marks<=100 : result="Great you are the topper"; break;
case marks>65 && marks<=90 : result="Congratulations you got First class"; break;
case marks>40&&marks<65:result="You got Second class"; break;
case marks>=30 &&marks<=40:result="You got Third class"; break;
case marks<30:result="Sorry you are fail better luck next time"; break;
Functions
Syntax: keyword
Function functionName(){
//statement Function block/function body
}
function goodStart(){
document.write("hello world");//Display on webpage
console.log("Test the code");// Display on console
}
goodStart();
goodStart();
goodStart();
goodStart();
---------------------------------------------
function sum(a,b){
console.log(a+b);
}
sum(10,20);
function product(a,b){
// console.log(a+b);
return a*b; //return
}
console.log(product(30,20));
Scopes
//--------------------------------Scopes----------------------------------------------
//global scope
var apple="red";
let orange="yellow";
const banana="green";
function fruits() {
document.write(apple);
document.write(orange);
document.write(banana);
}
--------------------------------
//function scope
var apple="red";
let orange="yellow";
Methods
➢ Array methods
➢ String methods
➢ Object methods
Array methods
• Push()
➢ Add one or more elements to the end of an array and returns the new length of
the array.
• Pop()
➢ Removes the last element from an array and returns that element.
• Shift()
➢ Removes the first element from the array and returns that element
• Unshift()
➢ Adds one or more elements to the beginning of an array and returns the new
length.
• concat()
➢ It combines two or more arrays and returns a new array.
• Join()
➢ Creates a new string by concatenating all the elements of an array and return a
string by a specified separator.
• Slice()
➢ Returns a shallow copy of a portion of an array into a new array.
• Splice()
➢ Changes the contents of an array by removing ,replacing or adding elements
//----------------Methods-----------------------------------
//Array methods
// let collections=["users", 18,true,10.5,{tej:"charan"}]; //Array
// ---------------Push()--------------------------
let sports=["cricket", "football"];
sports.push("shuttle",42);
document.write(sports+"<br>")
document.write(sports.length+"<br>")
//------------pop()------------------------
let books=["maths", "physics","chemistry"];
let result=books.pop();
document.write(books+"<br>")
document.write("deleted by pop method is ",result)
//---------shift()------------------------
let states=["banaglore", "andhra","chennai"]
let result2=states.shift();
document.write(states+"<br>");
document.write(states.length+"<br>");
document.write(result2);
//------------unshift()----------------------
let scores=[24,18,8,10,"tej"]
scores.unshift("jos",60)
document.write(scores)
//---------concat()-------------------
let scoress=[23,56,78,96]
let names=["tej","jos","cheery","sweety"]
let collect=scores.concat(names)
document.write(collect)
//----------join()--------------------
var team=[20,"tej","cheery","sweety",30,40];
var myteam=team.join("")
document.write(myteam)
var myteam1=team.join("/")
document.write(myteam1)
//---------------slice()---------------------
let students=["jose","tej","cheery",10,20,200,30]
console.log(students);
let res=students.slice(0,4)
document.write(res)
//-------------------splice()--------------------
document.write(mynumbers+"<br>")
// document.write(myNewNumbers)
document.write(myNewNumbers1)
//-----------------Index of------------------
let students=["tej","virat","jos","kohli","cherry"]
document.write(students.indexOf("jos"))
document.write(students.indexOf("priya"))
let res=students.indexOf("priya")
---
if(res==-1){
students.push("priya")
}
document.write(students)
//-----------forEach()-------------
let scores=[23,56,78,36,67,96]
let students=["priya","jose","tej","charan"]
students.forEach(function(item,index){
document.write(index+1+"."+item+"<br>")
})
scores.forEach(function(apple){
document.write(apple*2+"<br>")
document.write(apple+10+"<br>")
})
//if we call function as a parameter is called callback function
//--------------------reverse and search----------
let arr1 = [1, 5, 3, 4, 2, 6, 8, 7]
//reverse
document.write(arr1.reverse())
//search elements
document.write(arr1.includes(4))