Modern JavaScript Snippets ⚡ Short and effective JavaScript & TypeScript snippets for the modern-day developer.
Over200 carefully crafted snippets Modern JavaScript syntax Modern JavaScript APIs (Intl, URL, Navigator...) Strategically placed tabstops Prefixes created with exact-match in mind Auto-generated documentation Only JavaScript and TypeScript will be supported. Specific frameworks get their own extensions. No bloat.
The following is not mandatory, but could provide a nicer experience. Test them out and decide what works best for you.
Look for it in user settings, or edit the settings.json directly:
" editor.formatOnSave" :true ,// Tab complete snippets when their prefix match. Works best when 'quickSuggestions' aren't enabled." editor.tabCompletion" :" onlySnippets" Most of the code snippets are without semicolons (;
), except for where itallows for better tabstop management. Strings use single quotes ('
).
It's highly recommended to use these snippets along with Prettier/ESLint to haveyour code automatically formatted to your preference.
$1
,$2
,$3
specify cursor locations, in order in which tabstops will bevisited$0
denotes the final cursor positionMultiple occurrences of the same tabstop are linked and updated in sync Tabstops with default values →${1:name}
Tabstops with multiple values →${1|one,two,three|}
. Truncated in documentation, for easier viewing →${1|one,...|}
. Prefix Name Body ca
const assignment la
let assignment cas
const string assignment las
let string assignment caa
const array assignment cao
const object assignment dob
object destructuring dar
array destructuring
Prefix Name Body fn
function fna
async function async function $ { 1 :fn } ( $2 ) { $0 } nfn
named arrow function nfna
async named arrow function const $ { 1 :fn } = async ( $2 ) => { $0 } af
arrow function arr
arrow function arrow afa
async arrow function afb
arrow function with body afba
async arrow function with body iife
immediately-invoked function expression
Prefix Name Body iff
if statement ifel
if/else statement if ( $ { 1 :true } ) { $2 } else { $3 } ifei
if/else-if statement if ( $ { 1 :true } ) { $2 } else if ( $3 ) { $4 } el
else statement ei
else if statement ter
ternary operator tera
ternary expression assignment const $ { 1 :name } = $2 ?$3 :$4 sw
switch switch ( $1 ) { case $2 :$3 default :$0 } scase
switch case tc
try/catch tcf
try/catch/finally try { $1 } catch ( err ) { $2 } finally { $3 } tf
try/finally
Prefix Name Body flr
for loop (range) for ( let $ { 1 :i } = 0 ; $ { 1 :i } < $ { 2 :5 } ; $ { 1 :i } ++ ) { $0 } rfl
reverse for loop for ( let $ { 1 :i } = $ { 2 :iter } . length - 1 ; $ { 1 :i } >= 0 ; $ { 1 :i } -- ) { $0 } fin
for...in loop for ( let $ { 1 :key } in $ { 2 :array } ) { $0 } fof
for...of loop for ( let $ { 1 :item } of $ { 2 :items } ) { $0 } fofa
for await...of loop for await ( let $ { 1 :item } of $ { 2 :items } ) { $0 } wl
while loop dwl
do while loop
Prefix Name Body cs
class cse
class extends class $1 extends $ { 2 :Base } { $0 } csp
class proprety csc
class with constructor class $1 { constructor ( $2 ) { $0 } } csec
class extends with constructor class $1 extends $ { 2 :Base } { constructor ( $3 ) { $0 } } cst
class constructor get
getter set
setter set $ { 1 :property } ( $ { 2 :value } ) { $0 } gs
getter and setter get $ { 1 :property } ( ) { $0 } set $ { 1 :property } ( $ { 2 :value } ) { $0 } met
method meta
async method
Prefix Name Body aat
array.at fe
Array.forEach() $1 . forEach ( ( $ { 2 :item } ) => { $0 } ) map
Array.map() $1 . map ( ( $ { 2 :item } ) => $ { 3 } ) fmap
Array.flatMap() $1 . flatMap ( ( $ { 2 :item } ) => $ { 3 } ) reduce
Array.reduce() $1 . reduce ( ( $ { 2 :acc } , $ { 3 :curr } ) => { $0 } , $ { 4 :initial } ) reduceRight
Array.reduceRight() $1 . reduceRight ( ( $ { 2 :acc } , $ { 3 :curr } ) => { $0 } , $ { 4 :initial } ) filter
Array.filter() $1 . filter ( ( $ { 2 :item } ) => $ { 3 } ) find
Array.find() $1 . find ( ( $ { 2 :item } ) => $ { 3 } ) findl
Array.findLast() $1 . findLast ( ( $ { 2 :item } ) => $ { 3 } ) findi
Array.findIndex() $1 . findIndex ( ( $ { 2 :item } ) => $ { 3 } ) findli
Array.findLastIndex() $1 . findLastIndex ( ( $ { 2 :item } ) => $ { 3 } ) every
Array.every() $1 . every ( ( $ { 2 :item } ) => $ { 3 } ) some
Array.some() $1 . some ( ( $ { 2 :item } ) => $ { 3 } ) reverse
Array.reverse() sort
Array.sort( $1 . sort ( ( $ { 2 :a } , $ { 3 :b } ) => $4 ) group
Array.group() $1 . group ( ( $ { 2 :item } ) => $3 ) groupMap
Array.groupToMap() $1 . groupToMap ( ( $ { 2 :item } ) => $3 ) mapStr
Array.map() to string mapNum
Array.map() to number mapBool
Array.map() to boolean filterTruthy
Array.filter() truthy arfr
Array.from
Prefix Name Body im
import from module import { $2 } from '${1:module}' imd
import default import $ { 2 :thing } from '${1:module}' ima
import as import $ { 2 :* } as ${3 :name } from '${1:module}' imf
import file imp
import dynamic impa
await import dynamic imm
import meta ex
export exd
export default exf
export from export { $0 } from '${1:module}' exa
export all from exo
export object export const $ { 1 :name } = { $0} efn
export function exportfunction $ { 1 :name } ( $2 ) { $0 } edfn
export default function export default function $ { 1 :name } ( $2 ) { $0} enfn
export named arrow function export const $ { 1 :name } = ( $2 ) => { $0 }
Prefix Name Body fet
fetch await fetch ( $1 ) . then ( res => res . json ( ) ) feta
fetch assignment const $ { 1 | data , ...| } = await fetch ( $2 ) . then ( res => res . json ( ) ) npr
promise new Promise ( ( resolve , reject ) => { $0 } ) prr
Promise.resolve prj
Promise.reject then
promise then() $1 . then ( ( $ { 2 :value } ) => $0 ) catc
promise catch() thenc
promise then().catch() $1 . then ( ( $ { 2 :value } ) => $3 ) . catch ( ( $ { 4 :err } ) => $5 ) pra
Promise.all pras
Promise.allSettled pran
Promise.any
Literals, operators, expressions Grouping them all together for now
Prefix Name Body arr
array literal ob
object literal tl
template literal tle
template literal operation ns
new Set nm
new Map am
array merge om
object merge or
OR (||) and
AND (&&) lt
less than (<) lte
less than or equal to (<=) gt
greater than (>) gte
greater than or equal to (>=) nc
nullish coalescing (??) neq
strict non-equality (===) eq
strict equality (===) ora
logical OR assignment (||=) nca
nullish coalescing assignment (??=) plus
addition minus
subtraction mul
multiplication div
division mod
modulo inc
addition assignment sub
subtraction assignment mula
multiplication assignment diva
division assignment col
colon
Prefix Name Body oe
Object.entries ofe
Object.fromEntries ok
Object.keys ov
Object.values
Prefix Name Body pi
parse int parseInt ( $1 , $ { 2 | 10 , ...| } ) pf
parse float uniq
array of unique values seq
sequence of 0..n [ ...Array ( $ { 1 :length } ) . keys ( ) ] cp
copy to clipboard navigator . clipboard . writeText ( $1 ) nurl
new URL sp
url search params spa
url search params assignment const $ { 1 :params } = new URLSearchParams ( $2 ) spg
get search param sps
set search param
Prefix Name Body ret
return reo
return object rei
return object inline terr
throw error throw new $ { 1 | Error , ...| } ( $0 )
Prefix Name Body si
set interval setInterval ( ( ) => { $0 } , $ { 1 :1000 } ) st
set timeout setTimeout ( ( ) => { $0 } , $ { 1 :1000 } ) sim
set immediate prnt
process next tick process . nextTick ( ( ) => { $0 } )
Prefix Name Body jsp
JSON parse jss
JSON stringify JSON . stringify ( $ { 1 :value } ) jssf
JSON stringify (formatted) JSON . stringify ( $ { 1 :value } , null , 2 )
Prefix Name Body cl
console.log ci
console.info cdi
console.dir ce
console.error cw
console.warn ct
console.time console . time ( '$1' ) $0 console . timeEnd ( '$1' ) ctb
console.table clr
console.clear clm
console.log message clo
console.log object clc
console.log clipboard console . log ( { $CLIPBOARD} ) cll
console.log (labeled) cil
console.info (labeled) console . info ( '$1 :' , $1$2 ) cel
console.error (labeled) console . error ( '$1 :' , $1$2 ) cwl
console.warn (labeled) console . warn ( '$1 :' , $ { 2 :$1 } )
Prefix Name Body nd
new Date() nds
new Date() with date string new Date ( '${1:2023}-${2:|01,...|}-${3:31}' ) now
Date.now() tls
Date.toLocaleString() $1 . toLocaleString ( '${2|en-US,...|}' $3 )
Prefix Name Body qs
query selector $ { 1 :document } . querySelector ( '$2' ) qsa
query selector all $ { 1 :document } . querySelectorAll ( '$2' ) qsaa
query selector all as array [ ...$ { 1 :document } . querySelectorAll ( '$2' ) ] ael
event listener $ { 1 :document } . addEventListener ( '${2:click}' , ( e$3 ) => $0 ) qsae
query selector with event listener $ { 1 :document } . querySelector ( '$2' ) ?. addEventListener ( '${3:click}' , ( e$4 ) => $0 ) gid
get element by id $ { 1 :document } . getElementById ( '$2' )
Prefix Name Body req
require rqr
require assignment const $1 = require ( '${1:module}' ) mex
module.exports
Internationalization API
Prefix Name Body inf
Intl.NumberFormat new Intl . NumberFormat ( '${1|en-US,...|}' $3 ) . format ( $2 ) infs
Intl.NumberFormat style new Intl . NumberFormat ( '${1|en-US,...|}' , { style :'${3|decimal,...|}' , $4} ) . format ( $2 ) infc
Intl.NumberFormat as currency new Intl . NumberFormat ( '${1|en-US,...|}' , { style :'currency' , currency :'${3|USD,...|}' , $4} ) . format ( $2 ) infp
Intl.NumberFormat as percentage new Intl . NumberFormat ( '${1|en-US,...|}' , { style :'percent' , $3} ) . format ( $2 ) infu
Intl.NumberFormat as unit new Intl . NumberFormat ( '${1|en-US,...|}' , { style :'unit' , unit :'${3|acceleration-g-force,...|}' , unitDisplay :'${4|long,...|}' , $0} ) . format ( $2 ) idtf
Intl.DateTimeFormat new Intl . DateTimeFormat ( '${1|en-US,...|}' $3 ) . format ( $2 ) idtfs
Intl.DateTimeFormat with style new Intl . DateTimeFormat ( '${1|en-US,...|}' , { dateStyle :'$3' , $0} ) . format ( $2 )
Prefix Name Body aia
is array tof
typeof tofc
typeof check typeof $1 === '${2|undefined,...|}' iof
instanceof isnil
is nil nnil
is not nil isnan
is NaN nnan
is not NaN
Prefix Name Body desc
describe cont
context it
test (synchronous) ita
test (asynchronous) itc
test (callback) it ( '$1' , ( done ) => { $0 done ( ) } ) bf
before test suite bfe
before each test aft
after test suite afe
after each test
Prefix Name Body wlo
window.location wlh
window.location.href
Prefix Name Body us
'use strict' statement prs
process.server prc
process.client env
env variable envv
env variable (meta)
Available only where TypeScript is supported
Prefix Name Body cat
const assignment (typed) const $ { 1 :name } : ${2 :string } = $3 lat
let assignment (typed) let$ { 1 :name } : ${2 :string } = $3 caat
array assignment (typed) const $ { 1 :arr } : ${2 :string } [ ] = [ $0 ] caot
object assignment (typed) const $ { 1 :obj } : ${2 :object } = { $0 }
Prefix Name Body int
interface inte
interface extends interface $ { 1 :Model } extends $ { 2 :Base } { $0 } tp
type tpu
type union type $ { 1 :Model } = $ { 2 :string } | $ { 3 :number } tpi
type intersection
Prefix Name Body qst
query selector (typed) $ { 1 :document } . querySelector < $ { 2 | HTMLElement , ...| } > ( '$3' ) qsat
query selector all (typed) $ { 1 :document } . querySelectorAll < $ { 2 | HTMLElement , ...| } > ( '$3' ) qsaat
query selector all as array (typed) [ ...$ { 1 :document } . querySelectorAll < $ { 2 | HTMLElement , ...| } > ( '$3' ) ] gidt
get element by id (typed) $ { 1 :document } . getElementById < $ { 2 | HTMLElement , ...| } > ( '$3' )
# ensure Deno is installed# https://deno.land/manual@v1.29.1/getting_started/installation# generate .code-snippets and documentation npm run generate