当前位置:网站首页>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
边栏推荐
- 蛮力法/1~n的幂集 v4 递归
- Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
- Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
- 【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
- 轻便型FDW框架 for pb
- 冷酸灵,一个国产品牌IPO的30年曲折史
- 自注意力(self-attention)和多头注意力(multi-head attention)
- Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
- Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
- 连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
猜你喜欢

Talk about server performance optimization ~ (recommended Collection)

Magic tower game implementation source code and level generation

Elastic-Job的快速入门,三分钟带你体验分布式定时任务

Quick start to elastic job, three minutes to experience distributed scheduled tasks

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

App test case

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

You have to learn math to play art?

How to use Diablo immortal database

Diablo immortal wiki address Diablo immortal database address sharing
随机推荐
Meetup预告:Linkis新版本介绍以及DSS的应用实践
Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
Self attention and multi head attention
Codeforces Round #798 (Div. 2)
Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
游戏兼容性测试(通用方案)
Read the source code of micropyton - add the C extension class module (4)
Nanny tutorial: how to become a contributor to Apache linkis documents
ROS virtual time
Is Jiuzhou futures regular? Is it safe to open an account
揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
Attack and defense drill | network security "whistleblower": security monitoring
ros虚拟时间
简解深度学习Attention
Can you still have a wonderful life if you are laid off at the age of 35?
^29 event cycle model
^30H5 Web Worker多线程
分布式服务理论基础
Diablo immortal wiki address Diablo immortal database address sharing
Vertical bar of latex tips absolute value \left\right|