JavaScript notes

mtbink.com logo

JavaScript notes

  1. Format number:
    (123456789).toLocaleString()
    will returns:
    123,456,789
    .
  2. Padding 0:
    (123).toString().padStart(6, "0")
    will returns:
    000123
    .
  3. Padding spaces:
    (123).toString().padStart(6)
    will returns:
       123
    .
  4. Check is substring exists:
    ("abcde").includes("bc")
    will returns:
    true
    .
  5. Check if an item exists in an array:
    ["a","b","c"].includes("b")
    will returns:
    true
    .
  6. Check if string starts with:
    "abcde".startsWith("abc")
    will returns:
    true
    .
  7. Check if string ends with:
    "abcde".endsWith("cde")
    will returns:
    true
    .
  8. Check if the visitor is Malaysian:
    (new Date()).toString().includes("Malaysia")
    will returns:
    true
    if the visitor is Malaysian.
  9. TTS (Text-To-Speech):
    // ENTER SETUP
    // ===========
    let t = "hello world";
    // LEAVE SETUP
    // ===========
    
    let ss     = window.speechSynthesis;
    let voices = [];
    
    ss.onvoiceschanged = ()=>{
      voices = ss.getVoices();
    };
    voices = ss.getVoices();
    
    let ssu = new SpeechSynthesisUtterance(t);
    ssu.voice = voices[0];
    ss.speak(ssu);
    
    .
  10. Check if the DOM is loaded:
    document.addEventListener("DOMContentLoaded", (ev)=>{
      console.log("DOM fully loaded and parsed");
    });
    
    .
  11. Reload current page:
    history.go();
    
    .

Search the entire website:

Flag Counter
RSS