Skip to content

[js] 第77天 请快速答出此题的答案并解释:var x, y = 1; x + y = ? #532

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第77天 请快速答出此题的答案并解释:var x, y = 1; x + y = ?

Activity

ghost

ghost commented on Jul 2, 2019

@ghost
x // => undefined
y // => 1
x + y // => undefined + 1 => NaN

运算符 + 的implicit type conversion规则:

  • 若任何一侧是 string 或 object 则两边转换为 string 进行连接
  • 否则均转换为 number 并进行相加
  • 注意symbol 相加会 throw TypeError
changed the title [-][js] 第77天 请快速答出此题的答案并解释:var x, y = 1; x = y = ?[/-] [+][js] 第77天 请快速答出此题的答案并解释:var x, y = 1; x + y = ?[/+] on Jul 2, 2019
haizhilin2013

haizhilin2013 commented on Jul 2, 2019

@haizhilin2013
CollaboratorAuthor

@t532 题目出的有问题,修改了下,应该是 x + y

uuuutiger

uuuutiger commented on Jul 2, 2019

@uuuutiger

NaN

Konata9

Konata9 commented on Jul 2, 2019

@Konata9

在 chrome 试了下,答案:NaN

var x, y=1 是赋值操作。x 没有给值仅给出了定义,那么 x = undefined,而 y=1

undefined+1 = NaN

maczyt

maczyt commented on Jul 2, 2019

@maczyt

@t532 第一条规则 >若任何一侧是 string 或 object 则两边转换为 string 进行连接
关于object不是准确的。

举例:

const obj = {
  [Symbol.toPrimitive](hint) {
    console.log('hint', hint)
  }
};

'12' + obj; // 此时hint为default
12 + obj; // hint也为default

按照ES标准规则,hint为default则会依次调用valueOf和toString

所以不一定是两边转换为string进行连接

eg:

const obj = {
valueOf() {
  return 12;
},
toString() {
  return '21';
}
}

obj + 1; // 13
```
marspal

marspal commented on Jul 2, 2019

@marspal

@maczyt 正解

yxkhaha

yxkhaha commented on Jul 2, 2019

@yxkhaha
  • NaN
  • 原因:x只是声明了并未赋值,默认值为undefined, x+y ==> undefined + 1 = NaN
lookguy

lookguy commented on Jul 2, 2019

@lookguy

我居然以为会报错,undefined居然还能加一

xiangshuo1992

xiangshuo1992 commented on Jul 3, 2019

@xiangshuo1992
Contributor

居然改题目了,害我昨天想好久,总觉得x=y=?这不是赋值操作吗,有啥答案

haizhilin2013

haizhilin2013 commented on Jul 3, 2019

@haizhilin2013
CollaboratorAuthor

@xiangshuo1992 嘿嘿,我的错

poporeki

poporeki commented on Jul 4, 2019

@poporeki

NaN
变量x只是定义了并没有复制,此时为undefined,so 等于 undefined+1

Alex-Li2018

Alex-Li2018 commented on Sep 1, 2020

@Alex-Li2018

NaN

smile-2008

smile-2008 commented on Jan 27, 2021

@smile-2008
x // => undefined
y // => 1
x + y // => undefined + 1 => NaN

运算符 + 的implicit type conversion规则:

  • 若任何一侧是 string 或 object 则两边转换为 string 进行连接
  • 否则均转换为 number 并进行相加
  • 注意symbol 相加会 throw TypeError
1684838553

1684838553 commented on Dec 9, 2021

@1684838553

var x, y = 1; x + y = ?
x == undefined
y == 1
undefined 转成number类型为NaN
NaN + 1 == NaN

xiaoqiangz

xiaoqiangz commented on Jun 21, 2022

@xiaoqiangz

NaN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@Konata9@maczyt@xiaoqiangz

        Issue actions

          [js] 第77天 请快速答出此题的答案并解释:var x, y = 1; x + y = ? · Issue #532 · haizlin/fe-interview