Home  >  Article  >  Web Front-end  >  Example introduction to the difference between undefined and not defined in javascript_javascript skills

Example introduction to the difference between undefined and not defined in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:58:101074browse

After research, it was found that there is a big difference between the two. I don’t know what the difference is between the two in English. The research results are as follows
Test os: ubuntu
Test browser: chrome

Test case 1
console.log(a)
Report error ReferenceError: a is not defined

Test case 2
var a
console.log(a)
None An error is reported, but undefined is output.

Test case 2
var b = {};
console.log(b.a)
No error is reported, but undefined is output.

Test case 3
function c() {
}
var d = new c();
console.log(d.a)
No error is reported, but undefined is displayed
About typeof
Above All objects used for testing return String("undefined") using typeof
About OR operation ||
var d = a||3
Error a not defined
var a;
var d = a || 3
Result: d==3
var c = 22 || 44
c==22
var c = false || 33
c = = 33
if(x) {
}
Error reporting
var x
if(x) {
}
if will not be executed

Summary:
There are two kinds of undefined in js. Although typeof returns String("undefined")
After it is defined, but without any operation or an undefined property of an object, it can be used for operations. undefined , can be used as the second type of logical operation
variable, which is completely undefined and has no background (x.a, although x does not have an attribute, but it gives x face), it is an error type and cannot be used unless Using the typeof function
or operation in js does not return a bool value, but returns the last value calculated by js for both conditions, such as 1||2 1 is true, 2 does not need to be calculated, so 1||2 == 1 a||1 Error
var a; a|| 1 == 1;

I found a dictionary and translated it.
undefined can be translated as: unclear, that is, not Know what it is used for
and not defined can be translated into undefined

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn