当前位置:网站首页>01js basic null and undefined difference type conversion = = code block logical operator
01js basic null and undefined difference type conversion = = code block logical operator
2022-06-10 21:19:00 【It's the cloud!】
null And undefined difference
From the beginning of the design
undefined: When a variable is declared unassigned , The printout is undefined
null: When you want to declare an object but are not sure , Sure var obj = null
// When declaring a variable object {} And null difference
var obj1 = {}
var obj2 = null
if(obj1){
// Here the code will be executed
}
if(obj2){
// Here the code will not be executed
}Type conversion
string
Implicit conversion :+ As long as one is a string type ,+ It's splicing
Display conversion :String()
number
Implicit conversion :*/
Display conversion :Number()
boolean
Implicit conversion : If it is empty intuitively, it will turn to false,0/""/null/undefined/nan, Others are converted to true
Display conversion :Boolean()
== And === difference
== When making judgments , When two operation metadata types are different , Will convert the operand to number type , Then judge ,null Or object types
Situation 1 :null==undefined true
Situation two :NaN == NaN false
Situation three : Number type == String type String type ToNumber()
Situation four : Boolean Type will also ToNumber()
Situation five :Number || String == Object || null Will convert the complex type to the original type for ToPrimitive(obj/null)
// here null Although after ToPrimitive(), But it is returned directly by the function , There is no transformation , therefore false
console.log(123 == null) // false
//obj Object to carry out == When judging , Would call ToPrimitive() To the original object , How to implement the internal functions , We don't delve into , But we can rewrite
var obj = {
name : "zhao",
[Symbol.toprimitive](){
return 123
}
}
console.log(123 == obj) // true
=== Strict comparison does not carry out type conversion
Code block
stay js You can use curly braces to represent code blocks in , such as :if Judge for loop ... We can control code block execution through branch statements and loops
{
var name = "zhao"
var age = 18
}Be careful : Object is not a code block ,
Logical operators
Logic or a || b Just one for true, Namely true
The essence : Short circuit or res = a || b || c abc Are operands From left , To Boolean type , find true Just return the original data , Will not continue to judge downward , If all the operands are false Just return the original data of the last operand
function getMessageLength(message) {
var mes = message || " The default value is " // Most commonly used in development
return mes.length
}Logic and a && b As long as there is one false Namely false
The essence : Short circuit and res = a && b && c From left , To Boolean type , find false Just return the original data of the modified operand , Will not continue down , If it's all true, Just return the last operand metadata
var obj = {
name : "zhao",
friend : {
name: "li",
eating: function() {
console.log("eating")
}
}
}
obj && obj.friend && obj.friend.eating && obj.friend.eating() // It's safer
Logic is not !
To Boolean type var message = "hello world" !!message
边栏推荐
- torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
- ros虚拟时间
- CET-6 - Business English - the last recitation before the test
- Cut rope / integer split
- Brute force method /k integers out of 1~n integers
- Redis集群配置
- 【无标题】破目
- 蛮力法/u到v是否存在简单路径
- 一、Vulkan开发理论基础知识
- Is Zhongyan futures a regular platform in China? Is it safe to open an account? Want to open a futures account
猜你喜欢

保姆级教程:如何成为Apache Linkis文档贡献者

^30h5 web worker multithreading

Pytorch deep learning -- neural network convolution layer conv2d
![[generation confrontation network learning part I] classic Gan and its existing problems and related improvements](/img/5a/0a4015cd4dcc21afd16ca7f895d909.png)
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements

一、Vulkan开发理论基础知识

Redis缓存雪崩

Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.

2台电脑共享一套键盘鼠标

1、 Vulkan develops theoretical fundamentals

72. 编辑距离 ●●●
随机推荐
synergy: server refused client with our name
^30H5 Web Worker多线程
Nodejs: official document 3 Dgram stream
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
Redis缓存击穿
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
魔塔类游戏实现源码及关卡生成
"O & M youxiaodeng" self service account unlocking tool
pdf.js-----js解析pdf文件實現預覽,並獲取pdf文件中的內容(數組形式)
Leetcode advanced path - the first unique character in a string
Signal and system review 1
Read the source code of micropyton - add the C extension class module (2)
MySQL Basics
简解深度学习Attention
Power set V4 recursion of brute force method /1~n
20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
1、 Vulkan develops theoretical fundamentals
ros虚拟时间
[computer use] how to set software startup without auto startup
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)