× Search About Posts Code Music Links
Blank Try
experiment
lots
learn
more

JavaScript Snippets

Last site update: 1 Sep 2025

The VS Code snippets for JavaScript are less obvious than the CSS or HTML emmet ones but they can save a lot of typing. And unlike AI assistants they’re faster and predictable (once you know them.)

So here’s a partial list of some of the most useful .. first published on YAMLsite.

snippetwhat it doescode produced
alalert()alert(‘msg’);
coconfirm()confirm(‘msg’);
pmprompt()prompt(‘msg’);
aeaddEventListenerdocument.addEventListener(’load’, function (e) {
// body
});
clgconsole.logconsole.log()
gigetElementByIddocument.getElementById(‘id’);
qsquerySelectordocument.querySelector(‘selector’);
qsaquerySelectorAlldocument.querySelectorAll(‘selector’);
fnfunctionfunction methodName (arguments) {
// body
}
afnanonymous functionfunction(arguments) { // body }
anfnarrow function(params) => {

}
nfnnamed functionconst name = (params) => {

}
freforEach() looparray.forEach(currentItem => {

});
stosetTimeoutsetTimeout(() => {

}, delayInms);
stisetIntervalsetInterval(() => {

}, intervalInms);
caclassList.add()document.classList.add(‘class’);
ctclassList.toggle()document.classList.toggle(‘class’);
crclassList.remove()document.classList.remove(‘class’);
sasetAttribute()document.setAttribute(‘attr’, value);
raremoveAttribute()document.removeAttribute(‘attr’);
gagetAttribute()document.getAttribute(‘attr’);
celcreateElement()document.createElement(elem);
jpJSON.parseJSON.parse(obj);
jsJSON.stringifyJSON.stringify(obj);

See the full list