Skip to content

Latest commit

 

History

History
333 lines (220 loc) · 7.82 KB

File metadata and controls

333 lines (220 loc) · 7.82 KB

Setup and Usage Instructions

Table of Contents

  1. Prerequisites
  2. System Requirements
  3. Initial Setup
  4. Install Dependencies
  5. Available Commands
  6. Running Scripts
  7. Development Commands
  8. Troubleshooting
  9. Extending the Application
  10. Best Practices
  11. Documentation
  12. External Resources
  13. Version
  14. Last Updated

Prerequisites

System Requirements

  • Node.js: Version 16.13.1 or higher
  • Package Manager: npm (comes with Node.js)
  • Operating System: Any OS that supports Node.js (Windows, macOS, Linux)
  • Memory: 512MB RAM minimum (sufficient for running examples)

Knowledge Prerequisites

  • Basic understanding of JavaScript
  • Familiarity with command line/terminal

Initial Setup

1. Clone the Repository

git clone https://github.com/orassayag/es2021.git
cd es2021

Install Dependencies

Since this project has no external dependencies, this step is optional but good practice:

npm install

2. Verify Installation

Check Node.js version:

node --version

Should show v16.13.1 or higher.

3. Run the Examples

npm start

Available Commands

Development Commands

Running Scripts

Run All Examples:

npm start

Test Command (placeholder):

npm test

Setup Instructions

  1. Open the project in your IDE (VSCode recommended)
  2. Install dependencies:
    npm install
  3. Verify Node.js version (16.13.1 or higher):
    node --version

Running the Examples

Run All Examples

Execute all ES2021 feature demonstrations:

npm start

This will run through all the examples in index.js and display the output in your console.

ES2021 Features Covered

1. Numeric Separators

Makes large numbers more readable by using underscores as visual separators.

Example:

let billion = 1_000_000_000;
let price = 1_475_938.38;

2. String.prototype.replaceAll()

Replace all occurrences of a substring without using regular expressions.

Old way:

message.replace(/\+/g, ' ');

New way:

message.replaceAll('+', ' ');

3. Logical Assignment Operators

Three new operators that combine logical operations with assignment:

  • &&= (AND assignment): Assigns only if the left side is truthy
  • ||= (OR assignment): Assigns only if the left side is falsy
  • ??= (Nullish coalescing assignment): Assigns only if the left side is null or undefined

4. Promise.any()

Returns as soon as any of the promises fulfills. Rejects only if all promises reject.

Use case: When you need the first successful result from multiple sources.

5. Private Class Methods

Define truly private methods using the # prefix that cannot be accessed outside the class.

class Example {
  #privateMethod() {}
  publicMethod() {}
}

6. Private Getters and Setters

Create private accessors for class properties.

class Example {
  get #privateProp() {}
  set #privateProp(value) {}
}

7. WeakRef

Create weak references to objects that don't prevent garbage collection.

Use case: Caching scenarios where you want objects to be collected if memory is needed.

8. FinalizationRegistry

Execute cleanup logic when objects are garbage collected.

Use case: Release resources, close connections, or perform cleanup when objects are no longer needed.

Understanding the Code

The index.js file contains:

  • Clearly marked sections for each feature
  • Working examples with console output
  • Comments explaining the old vs new approaches
  • Edge cases and special behaviors

Modifying Examples

To experiment with the features:

  1. Open index.js
  2. Modify the example code
  3. Run npm start to see your changes
  4. Observe the console output

Browser Compatibility

ES2021 features are supported in:

  • Node.js 16+ (all features)
  • Chrome 90+
  • Firefox 88+
  • Safari 14.1+
  • Edge 90+

For older environments, consider using Babel transpilation.

Additional Resources

Notes

  • All examples are self-contained and can be run independently
  • The code demonstrates practical use cases, not just syntax
  • Some features (WeakRef, FinalizationRegistry) are advanced and should be used carefully
  • Private class fields and methods provide true encapsulation, unlike naming conventions

Troubleshooting

Common Issues and Solutions

Node.js Version Mismatch

Problem: "SyntaxError: Unexpected token" or similar errors

Solutions:

  1. Check your Node.js version:
    node --version
  2. Ensure you have Node.js 16.13.1 or higher installed
  3. Download the latest LTS version from nodejs.org

Module Not Found

Problem: "Error: Cannot find module"

Solutions:

  1. Ensure you're in the correct project directory
  2. Run npm install (even though there are no dependencies)
  3. Check that index.js exists in the project root

Permission Denied

Problem: "EACCES: permission denied"

Solutions:

  1. Ensure you have read/write permissions for the project directory
  2. Avoid using sudo unless necessary
  3. On Windows, run Command Prompt or PowerShell as Administrator if needed

Extending the Application

Adding New Examples

To add more ES2021 examples or extend existing ones:

  1. Open index.js
  2. Add your code in the appropriate section or create a new section
  3. Follow the existing code style and commenting patterns
  4. Run npm start to verify your changes work

Project Structure

All code is in index.js for simplicity. If the project grows, you could:

  • Split examples into separate files
  • Add a build system
  • Add testing framework

Best Practices

Learning Best Practices

  1. Read Carefully: Take time to understand each example
  2. Experiment: Modify the code to see different outcomes
  3. Take Notes: Write down what you learn
  4. Apply: Try using the features in your own projects
  5. Review: Come back later to reinforce your knowledge

Code Best Practices

  1. Follow the Style: Use Prettier for consistent formatting
  2. Add Comments: Explain what your code does
  3. Keep It Simple: Focus on one concept per example
  4. Test Changes: Always run npm start after making changes

Documentation

Additional Documentation

External Resources

Version

Version: 1.0.0

Last Updated

Last Updated: June 2026

Author