Node.js 14 introduces some new features and concepts. Highlights:
Nullish coalescing. This introduces
??
which is safer than using||
for assignment (as it only evaluates tofalse
fornull
orundefined
).Optional chaining. This introduces
?.
which allows safe access to deep keys on objects that may not exist.
The simplest AWS Lambda function that uses both these features:
// AWS_LAMBDA_JS_RUNTIME = 'nodejs14.x'
exports.handler = async event => {
const name = event?.queryStringParameters?.name ?? 'World'
return {
statusCode: 200,
body: `Hello, ${name}`,
}
}
Result:
// example.com/endpoint?name=John
'Hello, John'
// example.com/endpoint
'Hello, World'
Discussion (0)