In this blog, we'll explore the nullish coalescing operator ?? in JavaScript. We'll compare it with the logical OR operator ||, discuss its precedence, and provide practical examples to help you understand its usage. Let's dive in!
Nullish Coalescing Operator ??
The nullish coalescing operator ?? is used to provide a default value when dealing with null or undefined. It returns the right-hand operand when the left-hand operand is null or undefined. Otherwise, it returns the left-hand operand.
Syntax:
result = value1 ?? value2;
Example:
let user = null; let defaultUser = "Guest"; let currentUser = user ?? defaultUser; console.log(currentUser); // Output: "Guest"
Explanation:
- user is null.
- The ?? operator returns defaultUser because user is null.
Comparison with ||
The logical OR operator || returns the first truthy value it encounters. This means it will return the right-hand operand if the left-hand operand is falsy (false, 0, "", null, undefined, NaN).
Example:
let user = ""; let defaultUser = "Guest"; let currentUser = user || defaultUser; console.log(currentUser); // Output: "Guest"
Explanation:
- user is an empty string "" (falsy).
- The || operator returns defaultUser because user is falsy.
Key Differences
- ??: Only considers null and undefined as falsy values.
- ||: Considers all falsy values (false, 0, "", null, undefined, NaN).
Example:
let user = 0; let defaultUser = "Guest"; let currentUser1 = user ?? defaultUser; let currentUser2 = user || defaultUser; console.log(currentUser1); // Output: 0 console.log(currentUser2); // Output: "Guest"
Explanation:
- user is 0 (falsy for || but not for ??).
- The ?? operator returns 0 because user is not null or undefined.
- The || operator returns "Guest" because user is falsy.
Precedence
The nullish coalescing operator ?? has a lower precedence than most other operators, including the logical OR operator ||. This means that expressions involving ?? are evaluated after expressions involving ||.
Example:
let a = null; let b = false; let c = "Hello"; let result1 = a ?? b || c; let result2 = a || b ?? c; console.log(result1); // Output: "Hello" console.log(result2); // Output: "Hello"
Explanation:
- a ?? b returns false because a is null and b is false.
- false || c returns "Hello" because false is falsy and c is truthy.
- a || b returns false because a is null and b is false.
- false ?? c returns "Hello" because false is not null or undefined.
Practical Examples
Let's put everything together with some practical examples:
Using ?? for Default Values
function getUserName(user) { return user.name ?? "Guest"; } let user1 = { name: "Alice" }; let user2 = { age: 25 }; console.log(getUserName(user1)); // Output: "Alice" console.log(getUserName(user2)); // Output: "Guest"
Explanation:
- If user.name is null or undefined, the ?? operator returns "Guest".
Using || for Default Values
function getUserName(user) { return user.name || "Guest"; } let user1 = { name: "" }; let user2 = { age: 25 }; console.log(getUserName(user1)); // Output: "Guest" console.log(getUserName(user2)); // Output: "Guest"
Explanation:
- If user.name is falsy (e.g., ""), the || operator returns "Guest".
Summary
- ?? (Nullish Coalescing Operator): Returns the right-hand operand if the left-hand operand is null or undefined.
- || (Logical OR Operator): Returns the first truthy value it encounters.
- Precedence: The ?? operator has a lower precedence than the || operator.
Conclusion
The nullish coalescing operator ?? is a powerful tool in JavaScript for providing default values when dealing with null or undefined. By understanding its differences from the logical OR operator || and its precedence, you'll be able to write more robust and error-free code. Keep practicing and exploring to deepen your understanding of the nullish coalescing operator in JavaScript.
Stay tuned for more in-depth blogs on JavaScript! Happy coding!
以上是Nullish Coalescing Operator `??` in JavaScript的详细内容。更多信息请关注PHP中文网其他相关文章!

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务

本文展示了与许可证确保的后端的前端集成,并使用Next.js构建功能性Edtech SaaS应用程序。 前端获取用户权限以控制UI的可见性并确保API要求遵守角色库

JavaScript是现代Web开发的核心语言,因其多样性和灵活性而广泛应用。1)前端开发:通过DOM操作和现代框架(如React、Vue.js、Angular)构建动态网页和单页面应用。2)服务器端开发:Node.js利用非阻塞I/O模型处理高并发和实时应用。3)移动和桌面应用开发:通过ReactNative和Electron实现跨平台开发,提高开发效率。

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能