JS

cheatsheet
  1. JS variable
  2. const PI = 3.14159;
  3. JS loop
  4. for (let i = 0; i < 5; i++) {
                console.log("Index: " + i);
                }
  5. JS conditional statement
  6. let x = 10;
                if (x > 5) {
                console.log("x is greater than 5");
                } else {
                console.log("x is less than or equal to 5");
                }
  7. JS object
  8. let person = {
                name: "John",
                age: 30,
                city: "New York"
                };
  9. JS array
  10. let numbers = [1, 2, 3, 4, 5];
  11. JS function
  12. function greet(name) {
                console.log("Hello, " + name + "!");
                }
  13. JS array method (push)
  14. let fruits = ["apple", "banana"];
    fruits.push("orange");
  15. JS array method (pop)
  16. let fruits = ["apple", "banana", "orange"];
    fruits.pop();
  17. JS array method (forEach)
  18. let numbers = [1, 2, 3, 4, 5];
    numbers.forEach(function(number) {
    console.log(number);
    });
  19. JS string method (concat)
  20. let str1 = "Hello, ";
    let str2 = "world!";
    let greeting = str1.concat(str2);
  21. JS string method (toUpperCase)
  22. let str = "hello";
    let upperCaseStr = str.toUpperCase();
  23. JS string method (slice)
  24. let str = "Hello, world!";
    let slicedStr = str.slice(7);
  25. JS string method (indexOf)
  26. let str = "Hello, world!";
    let index = str.indexOf("world");
  27. JS string method (replace)
  28. let str = "Hello, world!";
    let newStr = str.replace("world", "mars");
  29. JS string method (split)
  30. let str = "apple, banana, orange";
    let fruits = str.split(", ");
  31. JS object method (Object.keys)
  32. let person = {
    name: "John",
    age: 30,
    city: "New York"
    };
    let keys = Object.keys(person);
  33. JS object method (Object.values)
  34. let person = {
    name: "John",
    age: 30,
    city: "New York"
    };
    let values = Object.values(person);
  35. JS object method (Object.entries)
  36. let person = {
    name: "John",
    age: 30,
    city: "New York"
    };
    let entries = Object.entries(person);
  37. JS Math object (Math.PI)
  38. let pi = Math.PI;
  39. JS Math object (Math.round)
  40. let rounded = Math.round(4.7);
  41. JS Math object (Math.floor)
  42. let roundedDown = Math.floor(4.7);
  43. JS Math object (Math.ceil)
  44. let roundedUp = Math.ceil(4.3);
  45. JS Math object (Math.random)
  46. let randomNumber = Math.random();
  47. JS Date object (new Date)
  48. let currentDate = new Date();
  49. JS Date object (getMonth)
  50. let month = currentDate.getMonth();
  51. JS Date object (getFullYear)
  52. let year = currentDate.getFullYear();
  53. JS Date object (getDay)
  54. let day = currentDate.getDay();
  55. JS Date object (getDate)
  56. let date = currentDate.getDate();
  57. JS Date object (getHours)
  58. let hours = currentDate.getHours();
  59. JS Date object (getMinutes)
  60. let minutes = currentDate.getMinutes();
  61. JS Date object (getSeconds)
  62. let seconds = currentDate.getSeconds();
  63. JS Date object (getMilliseconds)
  64. let milliseconds = currentDate.getMilliseconds();
  65. JS Date object (toDateString)
  66. let dateString = currentDate.toDateString();
  67. JS Date object (toISOString)
  68. let isoString = currentDate.toISOString();
  69. JS Date object (toLocaleDateString)
  70. let localeDateString = currentDate.toLocaleDateString();
  71. JS Date object (toLocaleTimeString)
  72. let localeTimeString = currentDate.toLocaleTimeString();
  73. JS Date object (toLocaleString)
  74. let localeString = currentDate.toLocaleString();
  75. JS Date object (UTC methods)
  76. let utcDate = currentDate.toUTCString();
  77. JS array method (forEach with index)
  78. let numbers = [1, 2, 3, 4, 5];
    numbers.forEach(function(number, index) {
    console.log("Index: " + index + ", Value: " + number);
    });
  79. JS array method (map)
  80. let numbers = [1, 2, 3, 4, 5];
    let doubled = numbers.map(function(number) {
    return number * 2;
    });
  81. JS array method (filter)
  82. let numbers = [1, 2, 3, 4, 5];
    let evenNumbers = numbers.filter(function(number) {
    return number % 2 === 0;
    });
  83. JS array method (reduce)
  84. let numbers = [1, 2, 3, 4, 5];
    let sum = numbers.reduce(function(accumulator, currentValue) {
    return accumulator + currentValue;
    }, 0);
  85. JS array method (every)
  86. let numbers = [2, 4, 6, 8, 10];
    let allEven = numbers.every(function(number) {
    return number % 2 === 0;
    });
  87. JS array method (some)
  88. let numbers = [1, 2, 3, 4, 5];
    let hasEven = numbers.some(function(number) {
    return number % 2 === 0;
    });