0% found this document useful (0 votes)
131 views

JavaScript Cheatsheet

Cheatsheet

Uploaded by

Sowmya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
131 views

JavaScript Cheatsheet

Cheatsheet

Uploaded by

Sowmya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Javascript Cheat Sheet JS Ble ETot Le ico) var num = [1, 2, 3, 4]; num.at(1) (eRe) num.push(5) // Add element to the end: [1, 2, 3, 4, 5] Due) LO} // remove last element: [1, 2, 3] Clee oa) // €i11 every element: [1, 1, 1, 1] Pease) // remove first element: [2, 3, 4] num.unshift(5) // Add element to beginning: [5, 1, 2, 3, 4 ] num. reverse() // sort in descending order: [4, 3, 2, 1] num. ineludes(2) // is array contains a specified value; true num.map( item => 2*item) // Map elementsz; [2, 4, 6, 8] num.filter( item => item > 2) // filter elemet: [3, 4] num.find(item => item > 2) // Find element ; 3(first match) num.every(item => item > 0) ena 3 num.findIndex(item => item === 2) // 1 num.reduce( (prev, curr) => prev + curr, 9) // 10 Chester O Na aS Se num.join(" * "); FE Soins 7h he seas a4 num.splice(2, @, "i", "p"); // add elements ; [1, 2, 'i', ‘p', 3, 4] num.slice(1,4); // slice elements from [1] to [4-1] num.sort(); // sort string alphabetically x.sort(function(a, b){return a - b}); // numeric sort x.sort(function(a, b){return b - a}); // numeric descending sort x.sort(function(a, b){return 6.5 - Math.random()}); // random sort Javascript Dates var d = new Date(); Date("2017-86-23") ; TA me Chee CoE Theol Date("2617"); // is set to Jan @1 Date("2617-66-23T12:00:00-09:45"); // YYYY-MM-DDTHH:MM:SSZ Date("June 23 2017"); ee OL Ch mh Date("Jun 23 2017 07:45:00 GMT+0530"); // time zone a = d.getDay(); // getting the weekday [tata Oi // day as a number (1-31) getDay(); TLC AMM tee) getFullYear(); Time) am baer a ae hO b a) getHours(); // hour (@-23) getMilliseconds(); // milliseconds (0-999) getMinutes(); metas eeD) getMonth(); 7/ month (6-11) getSeconds(); // seconds (8-59) aaa Oi TAM ey ole Mee ot) Errors Handlings try { Ae Solel a) alle undefinedFunction() ; catch(err) { // block to handle errors console. log(err.message) ; y Ame oan Pei etl) var x = document.getElementById("mynum").value; cto aan 8 if(x == "") throw "empty"; // error cases if(isNaN(x)) throw "not a number"; x = Number(x); if(x > 10) throw "too high"; catch(err) { // if there's an error document.write("Input is " + err); A alee console.error(err); // write the error in console Vi beurre nang Tameka a Tee Ro a eh ol document.write("
Done"); ) Javascript JSON var str = '{"names":[' + // crate JSON object '{"first" :"Hakun: lastN":"Matata" },' + '{"first":"Jane","lastN":"Doe" },' + Ga gall of pet toey GUA oils Fy -§ aM Fay ef Leg [Par myObj = JSON.parse(str); Perey document .write(my0bj.names[1].first) ; access var myJSON = JSON.stringify(my0bj); Sater aa ocalStorage.setItem("testJSON", myJSON) ; Eisele til Meet hee) text = localStorage.getItem("testJSON") ; toa kl (e Meh} Javascript Object var student = { object name Bee) ea BEE amo alee) eet ele ME RALLY Bsa) F-1 od- age:18, height:17@, SPO UCU MPMn a Toast) Ort // object function “ "+ this.lastName; 0 Me Sale ante hod Sasi mn Pal student[age]++; incrementing name = student. fullName(); call object function lee ene a eC Mouse Keyboard Frame Form Drag Animation Bee E Tilo] a aval cs) onclick, oncontextmenu, ondblclick, onmousedown, onmouseenter, onmouseleave, CTT hme ULC Lam LU EL LoL ae CTU Ett) ) yn cos l LL

You might also like