Essential JavaScript for React
Scope (var, let, const)
Summary:
var
Code
var numbers = [1, 2, 3, 4];for (var counter = 0; counter < numbers.length; counter++) {console.log(numbers[counter]);}console.log("at end: " + counter);Result
1234at end: 4
let
Code
let numbers = [1, 2, 3, 4];- for (var counter = 0; counter < numbers.length; counter++) {+ for (let counter = 0; counter < numbers.length; counter++) {console.log(numbers[counter]);}console.log('at end: ' + counter);Result
console.log('at end: ' + counter);^ReferenceError: counter is not defined
const
Code
const a = 1;a = 2;Result
TypeError: Assignment to constant variable.
Arrow Functions
Code
Using a function
let numbers = [1, 2, 3, 4];//verbosenumbers.forEach(function (n) {console.log(n);});Result
1234Using an arrow function
Code
let numbers = [1, 2, 3, 4];numbers.forEach((n) => console.log(n));Result
1234
Destructuring
Objects
Code
let person = {first: "Thomas",last: "Edison",age: 5,twitter: "@tom",};let { first, last } = person;console.log(first);console.log(last);Result
ThomasEdison
Assignment is left to right with an object literal.
Code
let person = {first: "Thomas",last: "Edison",age: 5,twitter: "@tom",};let { first: firstName, last: lastName } = person;console.log(firstName);console.log(lastName);Result
ThomasEdison
Arrays
Code
let numbers = [1, 2, 3];let [a, b, c] = numbers;console.log(a);console.log(b);console.log(c);Result
123
Classes
Constructors
Code:
If usingBabel
compiler:class Person {constructor(first, last) {this.first = first;this.last = last;}}let person = new Person("Ron", "Swanson");console.log(person.first + " " + person.last);
If using TypeScript (tsc
) compiler:class Person {first;last;constructor(first, last) {this.first = first;this.last = last;}}let person = new Person("Ron", "Swanson");console.log(person.first + " " + person.last);Result:
Ron Swanson
Methods
Code:
If usingBabel
compiler:class Person {constructor(first, last) {this.first = first;this.last = last;}getFullName() {return this.first + " " + this.last;}}let person = new Person("Ron", "Swanson");console.log(person.getFullName());
If using TypeScript (tsc
) compiler:class Person {first;last;constructor(first, last) {this.first = first;this.last = last;}getFullName() {return this.first + " " + this.last;}}let person = new Person("Ron", "Swanson");console.log(person.getFullName());Result:
Ron Swanson
Class Fields
Class Fields are only a stage-3 proposal so you need to install an additional plugin to use them.
The proposed feature of class fields is commonly used in React projects and is included in Create React App
's default configuration as well as the Run Code
extension we are using to run these examples.
This propsed feature is available in TypeScript without any additional configuration.
Code:
class Person {first;last;}let person = new Person();person.first = "Tom";person.last = "Haverford";console.log(person.first + " " + person.last);Result:
Craig McKeachie
Modules
First Module
Create file
src\my-module.js
or.\my-module.ts
Add the following code to
my-module.[js|ts]
export function myFunction() {return "myFunction was run.";}Code in
program.[js|ts]
- Auto import doesn't work in JavaScript, you need to use TypeScript
import { myFunction } from "./my-module";console.log(myFunction());Result
myFunction was run.
Another Module
Code in
my-module.[js|ts]
export function myFunction() {return "myFunction was run.";}function myPrivateFunction() {return "myPrivateFunction was run.";}let myObject = {myName: "I can access myObject's name",myMethod: function () {return "myMethod on myObject is running.";},};export { myObject };export const myPrimitive = 55;export class MyClass {myClassMethod() {return "myClassMethod on myClass is running.";}}Code in
program.[js|ts]
import { myFunction, myObject, myPrimitive, MyClass } from "./my-module";console.log(myFunction());console.log(myObject.myName);console.log(myObject.myMethod());console.log(myPrimitive);let myClass = new MyClass();console.log(myClass.myClassMethod());Result
myFunction was run.I can access myObject's namemyMethod on myObject is running.55myClassMethod on myClass is running.
Spread
Code
program.[js|ts]
let person = {first: "Thomas",last: "Edison",age: 5,twitter: "@tom",};let anotherPerson = {...person,age: 80,};console.log(anotherPerson);Result
{ first: 'Thomas', last: 'Edison', age: 80, twitter: '@tom' }Code
program.[js|ts]
const numbers = [1, 2, 3, 4];const moreNumbers = [...numbers, 5, 6, 7, 8];console.log(moreNumbers);Result
[1, 2, 3, 4,5, 6, 7, 8]