Home > Web Front-end > JS Tutorial > body text

Add !, +, -, ~, in front of JavaScript functions; introduction to the meaning of symbols

不言
Release: 2019-03-27 09:33:57
forward
4067 people have browsed it

The content of this article is about the meaning of adding !, ,-,~, ; symbols in front of JavaScript functions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

! Negate the true and false of the return value

 console.log(!function() {
        return;
    }()); // true undefined属于false,
 console.log(!function() {
        return "a";
    }()); // false 字符串a属于真
Copy after login

, - is a mathematical operation on the return value

console.log(+function() {
    return 5.1;
}()); // 5.1 
console.log(-function() {
    return 5.1;
}()); // -5.1
Copy after login

~ Perform a bitwise negation of the return value (all positive The bitwise negation of an integer is the negative of 1. The bitwise negation of all negative integers is the absolute value of 1. The bitwise negation of zero is -1)

console.log(~function() {
    return 5;
}()); // -6 
console.log(~function() {
    return -5;
}()); // 4 
console.log(~function() {
    return 0;
}()); // -1
console.log(~function() {
    return "5";
}()); // -6 按位取反也会对返回值进行强制转换,将字符串5转化为数字5,然后再按位取反
Copy after login

; is to prevent When the code is compressed, the previous code is not written; causing an error.

This article has ended here. For more other exciting content, you can pay attention to the JavaScript Video Tutorial column on the PHP Chinese website!

The above is the detailed content of Add !, +, -, ~, in front of JavaScript functions; introduction to the meaning of symbols. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!