The shortened form of checking works for
other data types as well. In this case,
this data is first converted to a boolean
type, and then compared with true
.
See an example:
let test = 3;
if (test) {
console.log('+++'); // it will work
} else {
console.log('---');
}
In fact, the above code is equivalent to the following:
let test = 3;
if (Boolean(test) === true) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = 3;
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = 'abc';
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = '';
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = 3 * 'abc';
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = null;
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = false;
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test;
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = 0;
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = '0';
if (test) {
console.log('+++');
} else {
console.log('---');
}
Without running the code, determine what will be output to the console:
let test = -1;
if (test) {
console.log('+++');
} else {
console.log('---');
}