Skip to content

šŸ‘©ā€šŸ’»JavaScript (ES2018) code snippets for VS Code

Notifications You must be signed in to change notification settings

Chalarangelo/vscode-javascript-snippets

Ā 
Ā 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Ā 

History

19 Commits
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 

Repository files navigation

JavaScript Snippets for VS Code

Version Downloads Rating

Setup

Just install this package from the Extension Marketplace, then make sure to add "editor.snippetSuggestions": "top" to your user settings to see these snippets on top in the suggestion popover.

Snippets

Snippets are optimized to be short and easy to remember.

Below is a list of all available snippets and the triggers of each one. The ā‡„ means the TAB key.

Declarations

vā‡„ var statement

var ${0}

v=ā‡„ var assignment

var ${1:name} = ${2:value};

lā‡„ let statement

let ${0}

l=ā‡„ let assignment

let ${1:name} = ${2:value};

dl=ā‡„ destructuring let assignment

let {${1:name}} = ${2:value};

coā‡„ const statement

const ${0}

co=ā‡„ const assignment

const ${1:name} = ${2:value};

dco=ā‡„ destructuring const assignment

const {${1:name}} = ${2:value};

Flow Control

ifā‡„ if statement

if (${1:condition}) {
	${0}
}

elā‡„ else statement

else {
	${0}
}

ifeā‡„ if/else statement

if (${1:condition}) {
	${0}
} else {
	
}

eiā‡„ else if statement

else if (${1:condition}) {
	${0}
}

terā‡„ ternary operator

${1:condition} ? ${2:expression} : ${3:expression};

flā‡„ for loop

for (let ${1:i} = 0, ${2:len} = ${3:iterable}.length; ${1:i} < ${2:len}; ${1:i}++) {
	${0}
}

rflā‡„ reverse for loop

for (let ${1:i} = ${2:iterable}.length - 1; ${1:i} >= 0; ${1:i}--) {
	${0}
}

fiā‡„ for in loop

for (let ${1:key} in ${2:array}) {
	if (${2:array}.hasOwnProperty(${1:key})) {
		${0}
	}
}

},

foā‡„ for of loop (ES6)

for (let ${1:key} of ${2:array}) {
	${0}
}

wlā‡„ while loop

while (${1:condition}) {
	${0}
}

tcā‡„ try/catch

try {
	${0}
} catch (${1:err}) {
	
}

tfā‡„ try/finally

try {
	${0}
} finally {
	
}

tcfā‡„ try/catch/finally

try {
	${0}
} catch (${1:err}) {
	
} finally {
	
}

swā‡„ switch case

switch (${1:expr}) {
	case ${2:value}:
		return $0;
	default:
		return;
}

Functions

fā‡„ anonymous function

function (${1:arguments}) {
	${0}
}

fnā‡„ named function

function ${1:name}(${2:arguments}) {
	${0}
}

iifeā‡„ immediately-invoked function expression (IIFE)

((${1:arguments}) => {
	${0}
})(${2});

faā‡„ function apply

${1:fn}.apply(${2:this}, ${3:arguments})

fcā‡„ function call

${1:fn}.call(${2:this}, ${3:arguments})

fbā‡„ function bind

${1:fn}.bind(${2:this}, ${3:arguments})

afā‡„ arrow function (ES6)

(${1:arguments}) => ${2:statement}

afbā‡„ arrow function with body (ES6)

(${1:arguments}) => {
	${0}
}

gfā‡„ generator function (ES6)

function* (${1:arguments}) {
	${0}
}

gfnā‡„ named generator function (ES6)

function* ${1:name}(${2:arguments}) {
	${0}
}

Iterables

seqā‡„ sequence of 0..n

[...Array(${1:length}).keys()]${0}

feā‡„ forEach loop

${1}.forEach((${2:item}) => {
	${0}
});

mapā‡„ map

${1}.map((${2:item}) => {
	${0}
});

reduceā‡„ reduce

${1}.reduce((${2:previous}, ${3:current}) => {
	${0}
}${4:, initial});

filterā‡„ filter

${1}.filter(${2:item} => {
	${0}
});

findā‡„ find

${1}.find(${2:item} => {
	${0}
});

Objects and Classes

olā‡„ object literal

{
	kv${0}
};

slolā‡„ same-line object literal

{ kv${0} };

kvā‡„ key/value pair

${1:key}: ${2:value},

cā‡„ class (ES6)

class ${1:name} {
	constructor(${2:arguments}) {
		${0}
	}
}

cexā‡„ child class (ES6)

class ${1:name} extends ${2:base} {
	constructor(${3:arguments}) {
		super(${3:arguments});
		${0}
	}
}

ctorā‡„ class constructor (ES6)

constructor(${1:arguments}) {
	super(${1:arguments});
	${0}
}

mā‡„ method (ES6 syntax)

${1:method}(${2:arguments}) {
	${0}
}

getā‡„ getter (ES6 syntax)

get ${1:property}() {
	${0}
}

setā‡„ setter (ES6 syntax)

set ${1:property}(${2:value}) {
	${0}
}

gsā‡„ getter and setter (ES6 syntax)

get ${1:property}() {
	${0}
}
set ${1:property}(${2:value}) {
	
}

pctorā‡„ prototypal constructor

var ${1:Class} = function(${2:arguments}) {
	${0}
};

protoā‡„ prototype method

${1:Class}.prototype.${2:method} = function(${3:arguments}) {
	${0}
};

oaā‡„ Object.assign

Object.assign(${1:dest}, ${2:source})

ocā‡„ Object.assign copy (shallow clone)

Object.assign({}, ${1:original}, ${2:source})

Returning values

rā‡„ return

return ${0};

rpā‡„ return Promise (ES6)

return new Promise((resolve, reject) => {
	${0}
});

rcā‡„ return complex value (such as JSX components)

return (
	${0}
);

Types

tofā‡„ typeof

typeof ${1:source} === '${2:undefined}'

iofā‡„ instanceof

${1:source} instanceof ${2:Object}

Promises

prā‡„ Promise (ES6)

new Promise((resolve, reject) => {
	${0}
})

thenā‡„ Promise.then

${1:promise}.then((${2:value}) => {
	${0}
})

catchā‡„ Promise.catch

${1:promise}.catch((${2:err}) => {
	${0}
})

ES6 Modules

exā‡„ export (ES6)

export ${1:member};

exdā‡„ export default (ES6)

export default ${1:member};

imā‡„ import module (ES6)

import ${1:*} from '${2:module}';

imaā‡„ import module as (ES6)

import ${1:*} as ${2:name} from '${3:module}';

Node.js

cbā‡„ Node.js style callback

(err, ${1:value}) => {${0}}

reā‡„ require

require('${1:module}');

relā‡„ require local

require('./${1:module}');

reqā‡„ require assignment

const ${1:module} = require('${1:module}');

reqlā‡„ require assignment local

const ${1:module} = require('./${1:module}');

dreqā‡„ destructuring require assignment

const {${1:module}} = require('${1:module}');

dreqlā‡„ destructuring require assignment local

const {${1:module}} = require('./${1:module}');

emā‡„ exports.member

exports.${1:member} = ${2:value};

meā‡„ module.exports

module.exports = ${1:name};

meoā‡„ module exports object

module.exports = {
	${1:member}
};

onā‡„ event handler

${1:emitter}.on('${2:event}', (${3:arguments}) => {
	${0}
});

BDD Testing (Mocha, Jasmine, etc.)

descā‡„ describe

describe('${1:description}', () => {
	${0}
});

contā‡„ context

context('${1:description}', () => {
	${0}
});

itā‡„ it

it('${1:description}', () => {
	${0}
});

itsā‡„ it synchronous

it('${1:description}', () => {
	${0}
});

itaā‡„ it asynchronous

it('${1:description}', (done) => {
	${0}
	done();
});

bfā‡„ before test suite

before(() => {
	${0}
});

bfeā‡„ before each test

beforeEach(() => {
	${0}
});

aftā‡„ after test suite

after(() => {
	${0}
});

afeā‡„ after each test

afterEach(() => {
	${0}
});

Console

clā‡„ console.log

console.log(${0});

ceā‡„ console.error

console.error(${0});

cwā‡„ console.warn

console.warn(${0});

cllā‡„ console.log labeled

console.log('${0}', ${0});

celā‡„ console.error labeled

console.error('${0}', ${0});

cwlā‡„ console.warn labeled

console.warn('${0}', ${0});

Timers

stā‡„ setTimeout

setTimeout(() => {
	${0}
}, ${1:delay});

siā‡„ setInterval

setInterval(() => {
	${0}
}, ${1:delay});

simā‡„ setImmediate

setImmediate(() => {
	${0}
});

ntā‡„ process nextTick

process.nextTick(() => {
	${0}
});

Miscellaneous

usā‡„ insert 'use strict' statement

'use strict';

About

šŸ‘©ā€šŸ’»JavaScript (ES2018) code snippets for VS Code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published