Javascript arrow function with prompt.

Discussion in 'Javascript' started by atomattacker, Jul 12, 2020.

  1. atomattacker

    atomattacker

    Joined:
    Jul 12, 2020
    Messages:
    2
    Likes Received:
    0
    let sum = () => {
    a=prompt();
    b=prompt();
    return a+b;
    }

    console.log(sum());

    I don't understand why the hell concatenate two numbers instead of sum them. Less understand why only a summary is a problem and multiply, extraction, dividing is OK. Can you help me please?
     
    atomattacker, Jul 12, 2020
    #1
    1. Advertisements

  2. atomattacker

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    114
    Likes Received:
    16
    A prompt() returns a STRING not a number so a+b is a .concatenates then. Use Number() to change the string to a number.
     
    sunfighter, Jul 12, 2020
    #2
    Ian likes this.
    1. Advertisements

  3. atomattacker

    atomattacker

    Joined:
    Jul 12, 2020
    Messages:
    2
    Likes Received:
    0
    Thank you very much!
     
    atomattacker, Jul 13, 2020
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.