当前位置:网站首页>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
边栏推荐
- LeetCode 进阶之路 - 69.X的平方根
- 【电脑使用】如何设置没有自启项的软件开机启动
- Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
- Redis缓存穿透
- 游戏兼容性测试(通用方案)
- App test case
- Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
- Meetup Preview: introduction to the new version of linkis and the application practice of DSS
- Li Kou 10821084 solution_ Question of SQL query type
- Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
猜你喜欢
![JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation](/img/42/bcda46a9297a544b44fea31be3f686.png)
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation

72. 编辑距离 ●●●

实用 | 如何利用 Burp Suite 进行密码爆破!

You have to learn math to play art?

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"

synergy: server refused client with our name

冷酸灵,一个国产品牌IPO的30年曲折史

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

Theoretical basis of distributed services

Redis cache avalanche
随机推荐
LeetCode 进阶之路 - 125.验证回文串
H.264中NALU、RBSP、SODB的关系
在手机上买基金安全吗?会不会被吞本金?
Pytorch deep learning -- convolution operation and code examples
Vissim仿真快速入门
Obtained network time + time zone (+8)
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
视频监控系统存储控件,带宽计算方法
Node (express) implements interfaces such as adding, deleting, modifying, and paging
Redis缓存穿透
In MySQL basics, MySQL adds an automatically added primary key (or any field) to an existing table
数据库系统概论 ---- 第一章 -- 绪论(重要知识点)
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
Quick start to elastic job, three minutes to experience distributed scheduled tasks
"O & M youxiaodeng" self service account unlocking tool
Diablo immortal wiki address Diablo immortal database address sharing
LeetCode 进阶之路 - 69.X的平方根
^30h5 web worker multithreading
Stacked bar graph move the mouse into the tooltip to prompt that the filter is 0 element, so as to realize custom bubbles
Read the source code of micropyton - add the C extension class module (4)