Categories
JavaScript Basics

JavaScript Cheat Sheet — Spread, Variables, and Conditionals

Spread the love

JavaScript is one of the most popular programming languages for web programming.

In this article, we’ll look at the basic syntax of modern JavaScript.

Spread Operator

We can use the spread operator to add properties from other objects to an object.

For instance, we can write:

const a = {
 frstName: "james",
 lastName: "smith",
}
const b = {
 ...a,
 lastName: "White",
 canSing: true,
}

Then b is:

{
  "frstName": "james",
  "lastName": "White",
  "canSing": true
}

Destructuring Nested Objects

We can destructure nested objects into variables.

For instance, we write:

const bob = {
  name: "bob jones",
  age: 29,
  address: {
    country: "Westeros",
    state: "Crownlands",
    pinCode: "500014",
  },
};
const {
  address: {
    state,
    pinCode
  },
  name
} = bob;

Then state is 'Crownlands' and pinCode is '500014' .

Exponent Operator

We can use the exponent operator to do exponentiation.

So we can write: 2 ** 8 and get 256.

Promises with finally

We can call finally to run code regardless of the result of a promise.

For example, we write:

Promise.resolve(1)
  .then((result) => {})
  .catch((error) => {})
  .finally(() => {})

if-else

We can use a if-else to write code that runs conditionally.

For instance, we can write:

if ((age >= 15) && (age < 19)) {
  status = "Eligible.";
} else {
  status = "Not eligible.";
}

If age is between 15 and 19 then the first block runs.

Otherwise, the 2nd block runs.

Switch Statement

We can use the switch statement to check for more than one case and run code if the value matches.

For instance, we write:

switch (new Date().getDay()) {
  case 6:
    text = "Saturday";
    break;
  case 0:
    text = "Sunday";
    break;
  default:
    text = "";
}

If getDay returns 6 then text is 'Saturday' .

If it returns 0, then text is 'Sunday' .

Otherwise, text is an empty string.

Assign Variables

We can assign variables with let :

let x

x is uninitialized.

We can initialize it:

let y = 1

We can assign it a string:

let a = 'hi'

We can assign it an array:

let b = [1,2,3]

And we can assign it a boolean:

let c = false;

And regex:

let d = /()/

Or assign it a function:

let e = function(){};

And we can create a constant:

const PI = 3.14;

We can assign multiple variables in a line:

let a = 1, b = 2, c = a + b;

Strict Mode

We can add 'use strict' to our code to make us write better JavaScript code.

For instance, we can’t write code like:

a = 1

since strict mode will stop global variables from being created.

Values

JavaScript has some values like booleans:

false, true

Numbers:

1.2, 0n001, 0xF6, NaN

Strings:

'foo', 'bar'

And reserved words for special values:

null, undefined, Infinity

Conclusion

JavaScript comes with various constructs to help us create programs.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *