
在本文中,我们将探讨“in”运算符以及如何在 JavaScript 中使用它。 in 运算符是 JavaScript 中的内置运算符,用于检查对象中是否存在特定属性。如果属性存在则返回 true,否则返回 false。
语法
prop in object
参数
此函数接受如下所述的以下参数 –
prop – 此参数保存表示属性名称或数组索引的字符串或符号。
object – 将检查该对象是否包含 prop .
立即学习“Java免费学习笔记(深入)”;
返回值 – 如果在对象中找到指定的属性,此方法将返回 true 或 false。
算家云
高效、便捷的人工智能算力服务平台
37 查看详情
示例 1
在下面的示例中,我们将使用 JavaScript 中的 ‘inin’ 运算符来查找属性是否存在。
# index .html
IN operator Welcome To Tutorials Point
// Illustration of in operator const array = ['key', 'value', 'title', 'TutorialsPoint'] // Output of the indexed number console.log(0 in array) //true console.log(2 in array) //true console.log(5 in array) //false // Output of the Value // you must specify the index number, not the value at that index console.log('key' in array) //false console.log('TutorialsPoint' in array) // false // output of the Array property console.log('length' in array)
输出
上述程序将在控制台中产生以下输出。
truetruefalsefalsefalsetrue
示例 2
在下面的示例中,我们演示了 in 运算符。
# index.html p>
IN operator Welcome To Tutorials Point
// Illustration of in operator const student = { name: 'Bill', class: 'IX', subjects: 'PCM', age: '16' }; console.log('name' in student); delete student.name; console.log('name' in student); if ('name' in student === false) { student.name = 'Steve'; } console.log(student.name);
输出
上述程序将在控制台中产生以下结果。
truefalseSteve
以上就是如何在 JavaScript 中使用“in”运算符?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/530165.html
微信扫一扫
支付宝扫一扫