js-modern
Write modern JavaScript and TypeScript for Salesforce Commerce platforms.
Before Writing Code
Always fetch the latest official documentation BEFORE writing JavaScript:
- MDN JavaScript Reference: WebSearch "MDN JavaScript reference ES6 2026" and WebFetch official docs
- TypeScript Documentation: WebSearch "TypeScript handbook 2026" and WebFetch official docs
- Platform-Specific Docs: WebSearch for "Salesforce B2C Commerce server-side JavaScript API 2026" (SFCC), "PWA Kit JavaScript TypeScript 2026" (PWA Kit), or "Lightning Web Components JavaScript 2026" (LWC)
This ensures you're using the latest syntax, APIs, and best practices for each platform.
Conceptual Architecture
Platform Runtime Constraints
This is the most critical concept. Three Salesforce Commerce platforms have fundamentally different JavaScript runtimes:
| Constraint |
SFCC Server-Side |
PWA Kit |
LWC (B2B) |
| Engine |
Rhino (Java-based) |
Node.js + V8 |
Browser (V8/SpiderMonkey) |
| Module system |
CommonJS (require) |
ES Modules (import) |
ES Modules (import) |
| async/await |
Not supported |
Fully supported |
Fully supported |
| Promises |
Not supported |
Fully supported |
Fully supported |
| Template literals |
Limited contexts |
Fully supported |
Fully supported |
| Arrow functions |
Supported |
Supported |
Supported |
| Destructuring |
Supported |
Supported |
Supported |
| Optional chaining |
Not supported |
Supported |
Supported |
| Classes |
Not supported |
Supported |
Required (extends LightningElement) |
| Decorators |
Not applicable |
Not applicable |
Required (@api, @track, @wire) |
| DOM access |
Not applicable |
Standard DOM |
Shadow DOM only (Locker/LWS) |
SFCC Server-Side JavaScript
The Rhino engine imposes strict limitations. Every SFCC script must account for these:
- Use
var or let/const (recent versions) -- verify against your instance
- No
async/await, no Promise -- all I/O is synchronous
- No ES modules -- use
require('dw/...') and module.exports
- Access platform APIs via
dw.* namespace (e.g., dw/catalog/ProductMgr)
- Use
module.superModule for cartridge overlay inheritance
// Pattern: SFCC module import
var ProductMgr = require('dw/catalog/ProductMgr');
// Fetch live docs for dw.* API signatures
// Pattern: module.superModule extension
var base = module.superModule;
// Fetch live docs for superModule behavior
PWA Kit JavaScript/TypeScript
Full modern JavaScript and TypeScript are available:
| Feature |
Stack |
| React |
17+ with hooks |
| TypeScript |
Full support, recommended |
| Node.js SSR |
Server-side rendering |
| Commerce SDK |
commerce-sdk-isomorphic |
| Bundler |
Webpack with code splitting |
// Pattern: PWA Kit component with Commerce SDK
// Fetch live docs for useProducts hook API
import { useProducts } from '@salesforce/commerce-sdk-react';
LWC JavaScript Constraints
LWC uses modern ES modules but enforces security boundaries:
| Allowed |
Not Allowed |
ES modules (import/export) |
Direct window/document manipulation |
async/await, Promises |
eval(), Function() constructor |
| Template literals, destructuring |
Third-party DOM libraries (jQuery) |
@api, @track, @wire decorators |
setTimeout(string) |
this.template.querySelector() |
document.getElementById() |
NavigationMixin |
window.location = ... |
// Pattern: LWC component skeleton
// Fetch live docs for decorator behavior
import { LightningElement, api, wire } from 'lwc';
Module Systems Comparison
| System |
Syntax |
Platform |
| CommonJS |
require() / module.exports |
SFCC server-side |
| ES Modules |
import / export |
PWA Kit, LWC |
| SFCC Overlay |
module.superModule |
SFCC cartridge extension |
| SFCC Wildcard |
require('*/cartridge/...') |
Cross-cartridge resolution |
Best Practices
Platform Adaptation
- Always verify which JS features your target platform supports before coding
- SFCC: Use CommonJS, avoid async/await, leverage
dw.* API
- PWA Kit: Use full modern JS/TS, React hooks, Commerce SDK
- LWC: Use ES modules, decorators, respect Lightning Web Security
Code Quality
- Use
const by default, let when reassignment is needed, never var (except SFCC if required)
- Prefer modern array methods (
map, filter, reduce) over imperative loops
- Use optional chaining (
?.) and nullish coalescing (??) where supported
- Destructure to extract values for cleaner, more readable code
TypeScript (PWA Kit)
- Use TypeScript for type safety and better IDE support
- Leverage Commerce SDK types for API responses
- Use
Partial<T>, Pick<T>, Omit<T> for flexible type definitions
Fetch MDN, TypeScript handbook, and platform-specific Salesforce docs for exact syntax support and API signatures before implementing.
1---2name: js-modern-23description: Write modern JavaScript and TypeScript — ES6+ features, async/await, modules, destructuring, optional chaining, TypeScript types. Covers SFCC server-side JS (CommonJS, limited ES6), PWA Kit (full modern JS/TS), and LWC JavaScript (Lightning Locker constraints). Use when writing JavaScript for Salesforce Commerce.4allowed-tools: Read, Write, Edit, Bash, Grep, Glob, WebSearch, WebFetch5metadata:6 source: https://skillmd.com/skills/orcaqubits/js-modern-27---89# js-modern1011Write modern JavaScript and TypeScript for Salesforce Commerce platforms.1213## Before Writing Code1415Always fetch the latest official documentation BEFORE writing JavaScript:1617- **MDN JavaScript Reference**: WebSearch "MDN JavaScript reference ES6 2026" and WebFetch official docs18- **TypeScript Documentation**: WebSearch "TypeScript handbook 2026" and WebFetch official docs19- **Platform-Specific Docs**: WebSearch for "Salesforce B2C Commerce server-side JavaScript API 2026" (SFCC), "PWA Kit JavaScript TypeScript 2026" (PWA Kit), or "Lightning Web Components JavaScript 2026" (LWC)2021This ensures you're using the latest syntax, APIs, and best practices for each platform.2223## Conceptual Architecture2425### Platform Runtime Constraints2627This is the most critical concept. Three Salesforce Commerce platforms have fundamentally different JavaScript runtimes:2829| Constraint | SFCC Server-Side | PWA Kit | LWC (B2B) |30|-----------|-----------------|---------|------------|31| **Engine** | Rhino (Java-based) | Node.js + V8 | Browser (V8/SpiderMonkey) |32| **Module system** | CommonJS (`require`) | ES Modules (`import`) | ES Modules (`import`) |33| **async/await** | Not supported | Fully supported | Fully supported |34| **Promises** | Not supported | Fully supported | Fully supported |35| **Template literals** | Limited contexts | Fully supported | Fully supported |36| **Arrow functions** | Supported | Supported | Supported |37| **Destructuring** | Supported | Supported | Supported |38| **Optional chaining** | Not supported | Supported | Supported |39| **Classes** | Not supported | Supported | Required (extends LightningElement) |40| **Decorators** | Not applicable | Not applicable | Required (@api, @track, @wire) |41| **DOM access** | Not applicable | Standard DOM | Shadow DOM only (Locker/LWS) |4243### SFCC Server-Side JavaScript4445The Rhino engine imposes strict limitations. Every SFCC script must account for these:4647- Use `var` or `let`/`const` (recent versions) -- verify against your instance48- No `async`/`await`, no `Promise` -- all I/O is synchronous49- No ES modules -- use `require('dw/...')` and `module.exports`50- Access platform APIs via `dw.*` namespace (e.g., `dw/catalog/ProductMgr`)51- Use `module.superModule` for cartridge overlay inheritance5253```javascript54// Pattern: SFCC module import55var ProductMgr = require('dw/catalog/ProductMgr');56// Fetch live docs for dw.* API signatures57```5859```javascript60// Pattern: module.superModule extension61var base = module.superModule;62// Fetch live docs for superModule behavior63```6465### PWA Kit JavaScript/TypeScript6667Full modern JavaScript and TypeScript are available:6869| Feature | Stack |70|---------|-------|71| React | 17+ with hooks |72| TypeScript | Full support, recommended |73| Node.js SSR | Server-side rendering |74| Commerce SDK | `commerce-sdk-isomorphic` |75| Bundler | Webpack with code splitting |7677```typescript78// Pattern: PWA Kit component with Commerce SDK79// Fetch live docs for useProducts hook API80import { useProducts } from '@salesforce/commerce-sdk-react';81```8283### LWC JavaScript Constraints8485LWC uses modern ES modules but enforces security boundaries:8687| Allowed | Not Allowed |88|---------|-------------|89| ES modules (`import`/`export`) | Direct `window`/`document` manipulation |90| `async`/`await`, Promises | `eval()`, `Function()` constructor |91| Template literals, destructuring | Third-party DOM libraries (jQuery) |92| `@api`, `@track`, `@wire` decorators | `setTimeout(string)` |93| `this.template.querySelector()` | `document.getElementById()` |94| `NavigationMixin` | `window.location = ...` |9596```javascript97// Pattern: LWC component skeleton98// Fetch live docs for decorator behavior99import { LightningElement, api, wire } from 'lwc';100```101102### Module Systems Comparison103104| System | Syntax | Platform |105|--------|--------|----------|106| CommonJS | `require()` / `module.exports` | SFCC server-side |107| ES Modules | `import` / `export` | PWA Kit, LWC |108| SFCC Overlay | `module.superModule` | SFCC cartridge extension |109| SFCC Wildcard | `require('*/cartridge/...')` | Cross-cartridge resolution |110111## Best Practices112113### Platform Adaptation114- Always verify which JS features your target platform supports before coding115- SFCC: Use CommonJS, avoid async/await, leverage `dw.*` API116- PWA Kit: Use full modern JS/TS, React hooks, Commerce SDK117- LWC: Use ES modules, decorators, respect Lightning Web Security118119### Code Quality120- Use `const` by default, `let` when reassignment is needed, never `var` (except SFCC if required)121- Prefer modern array methods (`map`, `filter`, `reduce`) over imperative loops122- Use optional chaining (`?.`) and nullish coalescing (`??`) where supported123- Destructure to extract values for cleaner, more readable code124125### TypeScript (PWA Kit)126- Use TypeScript for type safety and better IDE support127- Leverage Commerce SDK types for API responses128- Use `Partial<T>`, `Pick<T>`, `Omit<T>` for flexible type definitions129130Fetch MDN, TypeScript handbook, and platform-specific Salesforce docs for exact syntax support and API signatures before implementing.