Notes
Notes
------------------
𝐂𝐎𝐍𝐒𝐎𝐋𝐄 𝐎𝐔𝐓𝐏𝐔𝐓
''''''''''''''''''
-->one way of debugging is to use alert messages in browser
𝐚𝐥𝐞𝐫𝐭('𝐡𝐞𝐥𝐥𝐨 𝐰𝐨𝐫𝐥𝐝);
popup in browser window
instead of writing alerts in code file we can write them in console of browser
click on three dots , then click on more tools , then click on developer tools,
choose console.
now we can do this alert and other checking from the console itself.
and use command clear() to clear the console.
𝐕𝐀𝐑𝐈𝐀𝐁𝐋𝐄𝐒
'''''''''''
1.var
--> it is used in the beggining of the javascript
--> it is a gloabal variable
--> you cannot use another variable with the same name , even the previous variable
is within different codeblock.
-->so it is not commonly used in js nowadays
ex:- var pi= 3.14;
2.let
--> we can reassign values
ex:- let score =10;
3.const
--> we cannot reassign values , and need to be intialized at the time of creation.
ex:- const name = "Mahi";
𝐃𝐀𝐓𝐀 𝐓𝐘𝐏𝐄𝐒
'''''''''''''
1. strings , numbers , boolean , null , undefined
ex:-const name ="mahi";
const age = 20;
const rating = 4.5;
const iscool = true;
const x= null; //object data type
const y = undefined;
let z ; //undefined
2.touppercase
console.log(s.toLowerCase());
3.tolowercase
console.log(s.toUpperCase());
2.creating generally
const fruits = ['appples' ,'oranges' ,'pears'];
console.log(fruits);
note :- we can add new elemnts to our array ven though we have used const
5.adding new element by specifying next index
fruits[3]='grapes';
console.log(fruits)
6.adding the new elemnt directly at the end of array without specifying the index
fruits.push('mango')
console.log(fruits);
10.checking whether it is array or not
console.log(fruits); passing an array
console.log('hello') passing a string
𝐎𝐁𝐉𝐄𝐂𝐓 𝐋𝐈𝐓𝐄𝐑𝐀𝐋𝐒
'''''''''''''''''
const person ={
firstName : 'john',
lastName :'Doe',
age:20,
hobbies:['music','movies','sports'],
address :{
street:'main road',
city:'rajanagaram',
state:'AP'
}
}
𝐀𝐑𝐑𝐀𝐘 𝐎𝐅 𝐎𝐁𝐉𝐄𝐂𝐓𝐒
'''''''''''''''''''
const todos = [
{
id:1,
text:'take out trash',
isComp:true
},
{
id:2,
text:'Meeting with boss',
isComp:false
},
{
id:3,
text:'reading',
isComp:true
},
𝐉𝐒𝐎𝐍 𝐅𝐎𝐑𝐌𝐀𝐓
'''''''''''''
-->json stands for javascript object notation
--> it is a data format , usd mostly when working in API's
--> these are similar to object literals , but it doesnt use singl quotes
𝐋𝐎𝐎𝐏𝐒
'''''''
1.for loop
for(let i=0;i<10;i++){
console.log(`for loop number: ${i}`);
}
2.while loop
let i=0;
while(i<10){
console.log(i);
i++;
}
1.forEach
todos.forEach(function(todo){ todo is a prameter of function whihc stores the data
console.log(todo.text)
});
2.map
const todoText=todos.map(function(todo){
return todo.text;
});
console.log(todoText)
3.filter
const comptodos = todos.filter(function(todo){
return todo.isComp==true;
})
console.log(comptodos);
now we can a particular value in the array items returned by filter method by
mapping on the result
it is callled functionla programming
const comptext = todos.filter(function(todo){
return todo.isComp==true;
}).map(function(todo){
return todo.text;
})
console.log(comptext)
𝐂𝐨𝐧𝐝𝐢𝐭𝐢𝐨𝐧𝐚𝐥𝐬
''''''''''''
1.if else condition
double == : it just comapres the values , os it treats '10' and 10 as same
triple equal === : it compares the datatypes also so '10' not equal to 10
const x='10';
if(x===10){
console.log(`x is 10`)
}
else{
console.log(`x is not 10`)
}
2.ternary operators
const color = x>10?'red':'green';
console.log(color);
3.switches
switch(color){
case 'red':
console.log(`color is red`)
case 'blue':
console.log(`color is blue`)
defeault:
console.log(`colour is not red or blue`)
𝐅𝐔𝐍𝐂𝐓𝐈𝐎𝐍𝐒
'''''''''''
function addNum(num1,num2){
console.log(num1+num2)
}
addNum(5,4);
𝐀𝐫𝐫𝐨𝐰 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬
'''''''''''''''''
const addNums = (num1=1,num2=1) => {
return num1+num2;
}
console.log(addNums(3,4))
without paranthesis
const subtract = (num1=1,num2=1) => num1-num2
console.log(subtract(4,2))
this.getFullName = function(){
return `${firstName} ${lastName}`;
}
}
-->prototypes
Person.prototype.getBirthYear = function(){
return this.dob.getFullYear();
}
Person.prototype.getFullName =function(){
return `${this.firstName} ${this.lastName}`;
}
-->instantiate object
const person1 = new Person("john","doe","04-11-1920");
const person2 = new Person("mary","com","02/12/2012");
console.log(person1);
console.log(person2.firstName);
-->methods in constructor
console.log(person1.getBirthYear());
console.log(person1.getFullName());
𝐄𝐒𝟔 𝐂𝐥𝐚𝐬𝐬𝐞𝐬
'''''''''''
-->the above constructor functions are all part of es5
-->now with es6 we got the classes
class Person{
constructor(firstName , lastName , dob){
this.firstName=firstName;
this.lastName=lastName;
this.dob=new Date(dob);
}
getBirthYear(){
return this.dob.getFullYear();
}
getFullName(){
return `${this.firstName} ${this.lastName}`;
}
}
-->instantiate of object
const person1=new Person("mahi","dhani",'4-02-2012')
console.log(person1);
𝐖𝐢𝐧𝐝𝐨𝐰 𝐎𝐛𝐣𝐞𝐜𝐭 & 𝐃𝐎𝐌
''''''''''''''''''''''
-->it is parent object of browser
console.log(window)
window.alert(1);
𝐃𝐎𝐌 𝐒𝐞𝐥𝐞𝐜𝐭𝐢𝐨𝐧
''''''''''''''
-->single element selector
console.log(document.getElementById('my-form'));
const form=document.getElementById('my-form')
console.log(form)
console.log(document.querySelector('h1'))
-->multiple element
console.log(document.querySelectorAll('.item')) node list
console.log(document.getElementsByClassName('item')) html collection (we cannot
use array methods directly)
4.
ul.children[1].innerText='brad'
ul.lastElementChild.innerHTML='<h1>hello</h1>'
const btn=document.querySelector('.btn')
btn.style.background='red';
𝐄𝐯𝐞𝐧𝐭𝐬
'''''''
const btn = document.querySelector('.btn')
btn.addEventListener('click',(e) => {
e.preventDefault(); //to stop form atually submiting
console.log('click')
console.log(e)
console.log(e.target)
console.log(e.target.className)
document.querySelector('.items').lastElementChild.innerHTML = '<h1>hello</h1>'
})
𝐅𝐨𝐫𝐦 𝐒𝐜𝐫𝐢𝐩𝐭
'''''''''''