jquery fundamental notes

本文介绍了JavaScript中字符串到数字的类型转换方法,包括使用Number()函数和一元加号运算符的方式。此外,还探讨了比较运算符的行为以及如何创建对象字面量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://jqfundamentals.com/book/book.html#N2004B

 

Example 2.9. Forcing a string to act as a number

var foo = 1;
var bar = '2';

// coerce the string to a number
console.log(foo + Number(bar));





Example 2.10. Forcing a string to act as a number (using the unary-plus operator)

 

console.log(foo + +bar);



 

Example 2.12. Comparison operators

var foo = 1;
var bar = 0;
var baz = '1';
var bim = 2;

foo == baz;   // returns true; careful!







foo === parseInt(baz);   // returns true



 

Example 2.14. Values that evaluate to true

'0'; 
'any string';
[];  // an empty array
{};  // an empty object
1;   // any non-zero number


Example 2.15. Values that evaluate to false

0;
'';  // an empty string
NaN; // JavaScript's "not-a-number" variable
null;
undefined;  // be careful -- undefined can be redefined!

Example 2.31. Creating an "object literal"

var myObject = {
    sayHello : function() {
        console.log('hello');
    },

    myName : 'Rebecca'
};

myObject.sayHello();            // logs 'hello'
console.log(myObject.myName);   // logs 'Rebecca'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值