Welcome Coders

! Here is a every thing which is you want !

! Cheatsheet of every coding language is available on this website !


Java Cheatsheet

  1. On page script
    <script type="text/javascript">  ...</script>
                        
  2. Include external JS file
    <script src="filename.js"></script>
                        
  3. Delay - 1 second timeout
    setTimeout(function () {
    
    }, 1000);
                        
  4. Functions
    function addNumbers(a, b) {
    return a + b; ;
    }
    x = addNumbers(1, 2);
                        
  5. Edit DOM element
    document.getElementById("elementID").innerHTML = "Hello World!";
                        
  6. Output
    console.log(a); 
                        
  7. Comments
    /* Multi line
    comment */
    // One line
                        
  8. For Loop
    for (var i = 0; i < 10; i++) {
    document.write(i + ": " + i*3 + "
    "); }
  9. While Loop
    var i = 1;                      // initialize
    while (i < 100) {               // enters the cycle if statement is true
    i *= 2;                     // increment to avoid infinite loop
    document.write(i + ", ");   // output
    }
                        
  10. Do While Loop
    var i = 1;                      // initialize
    do {                            // enters cycle at least once
    i *= 2;                     // increment to avoid infinite loop
    document.write(i + ", ");   // output
    } while (i < 100) 
                        
  11. Break
    for (var i = 0; i < 10; i++) {
    if (i == 5) { break; }          // stops and exits the cycle
    document.write(i + ", ");       // last output number is 4
    }
                        
  12. Continue
    for (var i = 0; i < 10; i++) {
    if (i == 5) { continue; }       // skips the rest of the cycle
    document.write(i + ", ");       // skips 5
    }
                        
  13. If - Else
    if ((age >= 14) && (age < 19)) {        // logical condition
    status = "Eligible.";               // executed if condition is true
    } else {                                // else block is optional
    status = "Not eligible.";           // executed if condition is false
    }
                        
  14. Switch Statement
    switch (new Date().getDay()) {      // input is current day
    case 6:                         // if (day == 6)
    	text = "Saturday";          
    	break;
    case 0:                         // if (day == 0)
    	text = "Sunday";
    	break;
    default:                        // else...
    	text = "Whatever";
    } 
                        
  15. Strict mode
    "use strict";   // Use strict mode to write secure code
    x = 1; 
                        
  16. Values
    false, true                     // boolean
    18, 3.14, 0b10011, 0xF6, NaN    // number
    "flower", 'John'                // string
    undefined, null , Infinity      // special
                        
  17. Operators
    a = b + c - d;      // addition, substraction
    a = b * (c / d);    // multiplication, division
    x = 100 % 48;       // modulo. 100 / 48 remainder = 4
    a++; b--;           // postfix increment and decrement
                        
  18. Errors
    try {                           // block of code to try
    undefinedFunction();
    }
    catch(err) {                    // block to handle errors
    console.log(err.message);
    }
                        
  19. hrow error
    throw "My error message";    // throw a text
                        
  20. Input validation
    var x = document.getElementById("mynum").value; // get input value
    try { 
    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);          // output error
    console.error(err);                         // write the error in console
    }
    finally {
    document.write("
    Done"); // executed regardless of the try / catch result }