-
-
Save JettIsOnTheNet/1d03533dc219077b8f018b7259e7451a to your computer and use it in GitHub Desktop.
Syntax example scan sheet for Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript up to speed | |
// variables | |
let integerVar = 10; | |
const stringVar = "Hello, JavaScript!"; | |
// functions | |
function greet(name) { | |
return "Hello " + name + "!"; | |
} | |
// arrow function | |
const greetArrow = (name) => "Hello " + name + "!"; | |
// for loop | |
for (let i = 0; i < 5; i++) { | |
console.log("For loop iteration:", i); | |
} | |
// while loop | |
let count = 0; | |
while (count < 5) { | |
console.log("While loop iteration:", count); | |
count++; | |
} | |
// conditional if | |
if (integerVar > 5) { | |
console.log("Integer is greater than 5"); | |
} else if (integerVar === 5) { | |
console.log("Integer is 5"); | |
} else { | |
console.log("Integer is less than 5"); | |
} | |
// switch case | |
switch (true) { | |
case integerVar > 5: | |
console.log("Integer is greater than 5"); | |
break; | |
case integerVar === 5: | |
console.log("Integer is 5"); | |
break; | |
default: | |
console.log("Integer is less than 5"); | |
} | |
// data structures | |
let myArray = [1, 2, 3, 4, 5]; | |
let myObject = { a: 1, b: 2 }; | |
// array methods | |
let doubled = myArray.map((x) => x * 2); | |
let filtered = myArray.filter((x) => x > 3); | |
let sum = myArray.reduce((acc, x) => acc + x, 0); | |
// exception handling | |
try { | |
let result = 10 / 0; | |
if (!isFinite(result)) { | |
throw new Error("Math error: Division by zero"); | |
} | |
} catch (error) { | |
console.error("Caught an error:", error.message); | |
} | |
// asynchronous programming | |
async function fetchData() { | |
try { | |
let response = await fetch("https://api.example.com"); | |
let data = await response.json(); | |
console.log("Data fetched:", data); | |
} catch (error) { | |
console.error("Error fetching data:", error); | |
} | |
} | |
// DOM manipulation | |
document.getElementById("myButton").addEventListener("click", function () { | |
alert("Button clicked!"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment