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

JavaScript_CheatSheet__1746934285

This document is a JavaScript cheat sheet that covers various programming concepts such as loops, functions, and data types. It includes examples of for, while, and do-while loops, as well as how to manipulate the DOM and use conditional statements. Additionally, it highlights the use of strict mode and different data types in JavaScript.

Uploaded by

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

JavaScript_CheatSheet__1746934285

This document is a JavaScript cheat sheet that covers various programming concepts such as loops, functions, and data types. It includes examples of for, while, and do-while loops, as well as how to manipulate the DOM and use conditional statements. Additionally, it highlights the use of strict mode and different data types in JavaScript.

Uploaded by

Harsh Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JS CheatSheet

Basics ➤

Loops ↶ On page script


<script type="text/javascript"> ...
</script>
For Loop
for (var i = 0; i < 10; i++) { Include external JS file
document.write(i + ": " + i*3 + "<br />"); <script src="filename.js"></script>
}
var sum = 0; Delay - 1 second timeout
for (var i = 0; i < a.length; i++) { setTimeout(function () {
sum + = a[i];
} // parsing an array }, 1000);
html = "";
for (var i of custOrder) { Functions
html += "<li>" + i + "</li>"; function addNumbers(a, b) {
} return a + b; ;
}
While Loop
x = addNumbers(1, 2);
var i = 1; // initialize
while (i < 100) { // enters the cycle
Edit DOM element
i *= 2; // increment to avo
document.write(i + ", "); // output document.getElementById("elementID").innerHTML = "
}
Output
Do While Loop console.log(a); // write to the browse
var i = 1; // initialize document.write(a); // write to the HTML
do { // enters cycle at alert(a); // output in an alert
i *= 2; // increment to avo confirm("Really?"); // yes/no dialog, retu
document.write(i + ", "); // output prompt("Your age?","0"); // input dialog. Secon
} while (i < 100) // repeats cycle if
Comments
Break /* Multi line
for (var i = 0; i < 10; i++) { comment */
if (i == 5) { break; } // stops and ex // One line
document.write(i + ", "); // last output
}

Continue If - Else ⇵
for (var i = 0; i < 10; i++) {
if ((age >= 14) && (age < 19)) { // logical
if (i == 5) { continue; } // skips the re
status = "Eligible."; // execute
document.write(i + ", "); // skips 5
} else { // else bl
}
status = "Not eligible."; // execute
}

Switch Statement
Variables x switch (new Date().getDay()) { // input is cu
case 6: // if (day ==
var a; // variable
text = "Saturday";
var b = "init"; // string
break;
var c = "Hi" + " " + "Joe"; // = "Hi Joe"
case 0: // if (day ==
var d = 1 + 2 + "3"; // = "33"
text = "Sunday";
var e = [2,3,5,8]; // array
break;
var f = false; // boolean
default: // else...
var g = /()/; // RegEx
text = "Whatever";
var h = function(){}; // function object
}
const PI = 3.14; // constant
var a = 1, b = 2, c = a + b; // one line
let z = 'zzz'; // block scope loca

Strict mode Data Types ℜ


"use strict"; // Use strict mode to write secure
var age = 18; // number
x = 1; // Throws an error because variable
var name = "Jane"; // string

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy