- Format number:
(123456789).toLocaleString()
will returns: 123,456,789
.
- Padding 0:
(123).toString().padStart(6, "0")
will returns: 000123
.
- Padding spaces:
(123).toString().padStart(6)
will returns: 123
.
- Check is substring exists:
("abcde").includes("bc")
will returns: true
.
- Check if an item exists in an array:
["a","b","c"].includes("b")
will returns: true
.
- Check if string starts with:
"abcde".startsWith("abc")
will returns: true
.
- Check if string ends with:
"abcde".endsWith("cde")
will returns: true
.
- Check if the visitor is Malaysian:
(new Date()).toString().includes("Malaysia")
will returns: true
if the visitor is Malaysian.
- 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);
.
- Check if the DOM is loaded:
document.addEventListener("DOMContentLoaded", (ev)=>{
console.log("DOM fully loaded and parsed");
});
.
- Reload current page:
history.go();
.
- Regular expression capture group
(.*?) and do action on the captured group:
function replacer(match, capture1, offset, original_string) {
let n = parseFloat(capture1);
return "output: " + n*2;
}
let s = "[tag]3[/tag]";
s.replace(/\[tag\](.*?)\[\/tag\]/gi, replacer);
will returns: output: 6
.