Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

This is a crash course in JavaScript. It is intended for people who already have a bit of programming experience in other languages.

This will hopefully give a basic idea of the most-commonly-used language features, but it is not indended to be a comprehensive guide.

This guide was last updated in August 2016.

Basic syntax

To declare a variable called foo:

let foo = 5;

To declare a function called addTwoNumbers, which accepts two parameters and adds them together:

function addTwoNumbers (param1, param2) {
  return param1 + param2;

// or equivalently:

let addTwoNumbers = function (param1, param2) {
  return param1 + param2;

To call the function addTwoNumbers with 3 and 5:

addTwoNumbers(3, 5); // <-- this will be 8

To print something to the console, use the built-in console.log function:

console.log('the answer is ' + answer);

Strings can be either single-quoted or double-quoted.

let string1 = "This is a string.";
let string2 = 'This is also a string';


An object is a collection of key-value pairs, without any particular ordering.

Objects are created like this:

let exampleObject = {key1: value1, key2: 'value2'};

The keys have to be strings*, but the values don't:

let myObject = {foo: 1, bar: 2, baz: 3};

In the example above, myObject has 3 keys (foo, bar, and baz) with corresponding values 1, 2, and 3 respectively.

To access the values of an object, use a dot followed by the key name.

myObject.baz // <-- this will be 3

To set values on an object:

myObject.qux = 5; // Creates a key `qux` on `myObject` with value 5.


An array is an ordered list of elements. To create an array, separate the elements with commas, and surround the whole thing with square brackets.

let myArray = ['foo', 'bar', 'baz'];

To add an element to an array, use the built-in push function.


console.log(myArray); // prints out ['foo', 'bar', 'baz', 'qux']

To get the element of an array at a given index, use square brackets:

myArray[2] // <-- this will be 'baz'

To get the length of an array, use the .length property.

myArray.length // <-- this will be 4

Control flow

if statements:

if (someCondition) {
  // code
if (someCondition) {
  // code
} else {
  // other code


while (someCondition) {
  // code

To check whether two values are equal to each other, use triple equals: ===

if (myVariable === 5) {
  // etc.

There is also a double-equals operator (==), which is similar but can be confusing because it performs type coersion. Generally, it's best to use === unless you know what you're doing.

Modules and Node.js

This section only applies to Node.js. Node.js is a JavaScript runtime environment which allows you to run javascript programs on a server or in a script. (JavaScript can also be run in a web browser, or a few other environments.)

To export code for use in another file, use the built-in module.exports object.

// e.g. in a file called myModule.js:

module.exports = {foo: 5, bar: 7};

Then to import the code from a different file, use the built-in require function with the module's filepath.

// in another file
var myImportedModule = require('./myModule.js');
console.log(myImportedModule); // logs {foo: 5, bar: 7}
console.log(; // logs 5

Miscellaneous syntax:

This is syntax that you don't really need to have in order to write basic JavaScript, but can be helpful to know if you're reading someone else's code.

var, const

var and const are alternate ways of declaring variables. There are a few differences, but for the most part they're the same as let.


The => operator is an alternate way of expressing functions, See arrow functions.

Basically, the following statements are almost equivalent:

let myFunction = function (param1, param2) {
  // code goes here

// ---

let myFunction = (param1, param2) => {
  // code goes here

There are a few subtle differences between the two functions, specifically if you're using the special variable this. However, most of the time you don't have to worry about the difference.

For more detailed documentation on these concepts, I recommend MDN.


This comment has been minimized.

Copy link

@Masum-Osman Masum-Osman commented Feb 2, 2019

WOW, in a simple word, WOW...
that's what i was looking for...


This comment has been minimized.

Copy link

@guidec guidec commented Mar 3, 2019

This is great. Super straightforward, and highlights the important aspects specific to js.


This comment has been minimized.

Copy link

@DukeRupert DukeRupert commented Jul 4, 2019

Excellent summary! Both concise and helpful which is a rare combination.


This comment has been minimized.

Copy link

@mchatzis mchatzis commented Feb 23, 2021

Thanks a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment