Appendix A. Modern JavaScript Syntax

Some of the code samples in this book use modern JavaScript syntax. If you’re not familiar with this syntax, don’t worry—it’s a pretty straightforward translation from the JavaScript you might be accustomed to.

ECMAScript 5, or ES5, is the JavaScript language specification with the broadest adoption. However, there are many compelling language features introduced in ES6, ES7, and beyond. React Native uses Babel, the JavaScript compiler, to transform our JavaScript and JSX code. One of Babel’s features is its ability to compile newer-style syntax into ES5-compliant JavaScript. This enables us to use language features from ES6 and beyond throughout our React codebase.

let and const

In pre-ES6 JavaScript, we use var to declare variables.

In ES6, there are two additional ways to declare variables: let and const. A variable declared with const cannot be reassigned; that is to say, the following is invalid:

const count = 2;
count = count + 1; // BAD

Variables declared with let or var may be reassigned. A variable declared with let may only be used in the same block as it is defined.

Some of the examples in this book still use var, but you’ll also see let and const. Don’t worry about the distinctions too much.

Importing Modules

We could use CommonJS module syntax to export our components and other JavaScript modules (Example A-1). In this system, we use require to import other modules, and assign a value to module.exports in order to make a file’s ...

Get Learning React Native, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.