JS Notes
JS Notes
console.log(first.hasAttribute('class'))
console.log(first.hasAttribute('style'))
it return true/false if this attribute use whose id is ‘first’
Class 1 01.Installing VS Code, Extensions &
After installing VSCode add extension “Liver server”
</body>
</html>
5) Live server run program with the help of live server. The main benefit of live server
option is that no need to refresh the web page every time. Once we save the code,
automatically changes update the webpage.
Class 2 02.Console Logs, Errors, Warnings & More –
Console.log show the message on console. Should write in between script tag.
6) Add java script file in html page used to take input from external file. If we have 2-3
line script code then we can directly put in html file but if we have too large code then
need to create external file.
(Use script:src and press enter key)
let strF='8966.234';
console.log(strF, typeof strF);
strF=parseFloat('122.453');
console.log(strF, typeof strF); //122.453
console.log(strF.toFixed(2), typeof strF); //122.45
console.log(strF.toFixed(), typeof strF); //122
console.log(strF.toFixed(15), typeof strF);
//122.453000000000003
//TYPE COERCION
let myStr1='5566';
let myStr2=50;
console.log(typeof(myStr1 + myStr2),(myStr1 + myStr2)); //string
556650
class 6
let greeting="Hello";
let fName="Naresh";
//let merge; --> if we use this then shows error while using concat
method
let merge='';
console.log(greeting.toLocaleLowerCase()); //hello
console.log(greeting.toUpperCase());//HELLO
console.log('Length of First Name '+ fName.length);//Length of First Name
6
merge=merge.concat(greeting,fName); //HelloNaresh
console.log(merge);//e
console.log(merge[1]); // 2 access charector using index number
//if string is not present then return -1
console.log(merge.indexOf('llo')); //2
console.log(merge.charAt(5)); //N
console.log(merge.lastIndexOf('e')); //8
console.log(merge.lastIndexOf('e453')); //-1
console.log(merge.endsWith('esh')); //true
console.log(merge.includes('are')); //true
console.log(merge.substring(1,7));//elloNa
console.log(merge.slice(-4));//resh
console.log(merge.slice(0,-4)); //HelloNa
console.log(merge.split('e')); //['H', 'lloNar', 'sh']
console.log(merge.replace('e','E'));//replace only first occurence --
>HElloNaresh
console.log(merge.replaceAll('e','E'));//replace all occurence --
>HElloNarEsh
//TEMPLATE LITERALS
//using back ticks we can use html tags also
let fruits1='Apple';
let fruits2='Mango';
let test=`This is ${fruits1} and I like ${fruits2}`;
console.log(test); //This is Apple and I like Mango
Output
hello
tut6.js:6 HELLO
tut6.js:7 Length of First Name 6
tut6.js:10 HelloNaresh
tut6.js:11 e
tut6.js:13 2
tut6.js:14 N
tut6.js:15 8
tut6.js:16 -1
tut6.js:17 true
tut6.js:18 true
tut6.js:19 elloNa
tut6.js:20 resh
tut6.js:21 HelloNa
tut6.js:22 (3) ['H', 'lloNar', 'sh']
tut6.js:23 HElloNaresh
tut6.js:24 HElloNarEsh
tut6.js:31 This is Apple and I like Mango
class 7
Array and its properties and objects
shift add the values at the beginning of array
unshift add the element at the last of the array
splice remove the element from the array start from the specified index and remove the total
no. of element specified with function
//Array and their properties
const marks=[10,40,80,33,60];
let fruits=['Apple','Mango','orange'];
let mixed= [2,5,['Naresh',56],'Badve',90];
console.log(marks); //[10, 40, 80, 33, 60]
console.log(fruits); //['Apple', 'Mango', 'orange']
console.log(mixed); //[2, 5, Array(2), 'Badve', 90]
marks[1]=400;
console.log(marks);//[10, 400, 80, 33, 60]
//create object
let details={
fName:'Naresh',
lName:'Badve',
age:38,
city:'Nagpur'
}
console.log(details);//{fName: 'Naresh', lName: 'Badve', age:
38, city: 'Nagpur'}
console.table(details);// print details in the form of table
Points to remember:
1. Access values and key using object.key function (Simple for
loop)
2. //WAP to print the marks of a students on an object using for loop
3. let marks={
4. nares:67,
5. vishu:90,
6. vrishang:99,
7. vritika:100
8. }
9. console.log(Object.keys(marks).length);
10. for(let i=0;i<Object.keys(marks).length;i++){
11. console.log(Object.keys(marks)[i] + " : "+
marks[Object.keys(marks)[i]]);
12. }
2. Arrow function
4. fine the mean of 5 nummber
Avg =(a,b,c,d,e)=>{
return ((a+b+c+d+e)/5)
}
console.log(Avg(4,8,6,3,2));
3. Template literals
//Template literals
let sentence=`${name1} is the father of ${name2}`
console.log(sentence);
6. Array methods