简体   繁体   中英

Using the “+” (plus) and the “,” (comma) in javavascript prompt(), document.write() and console.log()

I am a bit confused as to how JavaScript is interpreted for the following:

1.

n = prompt("Please enter a number to calculate factorial " + "Now!", "Delete this first!" + " ...And this :(", "Hello");

I assume that the comma "," is used to separate the arguments sent to the function, and that prompt() takes either one(1) or two(2) arguments and discards the third one: "Hello" ?

2.

How are "," commas interpreted in the document.write() function and the console.log() function. 

Are they used to separate arguments? Can these functions take an infinite number of arguments?

3.

In these examples:

console.log("1.) expression " , (1<2).toString() + 3>2);

console.log("2.) expression " , (3<2).toString() , 3>2);

console.log("3.) expression " + (3>2).toString() + 3>2);

console.log("4.) expression " , 3<2);

console.log("5.) expression " + 3<2);

console.log("6.) expression " + n + 3<2);

OUTPUTS:

1.) expression  false

2.) expression  false true

3.) false

4.) expression  false

5.) false

6.) false

I'm guessing the "+" is treated as part of the expression and not to concatenate strings when it encounters another operator?

In the first expression, was (1<2).toString() added to 3 ? What kind of type and what value was it converted to? Why does it evaluate to false ?

Can I assume that the document.write() and console.log() functions treat the "," (comma) differently than other functions?

  1. Regarding prompt

I assume that the comma "," is used to separate the arguments sent to the function, and that prompt() takes either one(1) or two(2) arguments and discards the third one: "Hello" ?

prompt takes two arguments. Any extras are ignored as is the case of all functions in JavaScript that don't use the arguments object or rest parameters to change behaviour for a variable number of arguments.

function x ( a ) { return a; }
x( 'this', 'is', 'perfectly' ,'valid' );
  1. Regarding document.write and console.log

Are they used to separate arguments? Can these functions take an infinite number of arguments?

Yes, a comma in a function call is always used to separate arguments. As far as I know both functions make use of a variable amount of arguments, but it is likely implementation specific.

  1. I'll add some parenthesis to help you understand the output from those lines:

console.log("1.) expression " , ( (1<2).toString() + 3 ) > 2 );
console.log("2.) expression " , (3<2).toString() , 3>2);
console.log( ( "3.) expression " + (3>2).toString() + 3 ) > 2);
console.log("4.) expression " , 3<2);
console.log( ( "5.) expression " + 3 ) <2);
console.log( ( "6.) expression " + n + 3 ) <2);

Can I assume that the document.write() and console.log() functions treat the "," (comma) differently than other functions?

You would be wrong to assume that, but I won't try and stop you.

Disclaimer I am writing this from my understanding of Javascript and how it works. I was never formally taught Javascript, I did not study this language in college, and I've never looked this up. If it is wrong, please understand that this is my(working) knowledge of how Javascript works.

The Functions

There's quite a bit to talk about for your question. First and foremost is that each of those functions are different in what they require or will accept. Prompt() accepts only two arguments: the text it outputs and the "Default text" (the text that shows up in the prompt originally). You can put in more arguments, but the function isn't coded to do anything with them so nothing happens.

The console.log() function, much like the document.write() function take a variable (and theoretically infinite) number of arguments. Each argument is evaluated and passed to those functions. Console.log and document.write will both simply output those expressions to their respective locations (the Javascript Console or the document object)

The + Operator

This is the trickier part to answer, and is again written from entirely my own understanding of Javascript. The + operator in Javascript is dual function. It works as both the mathematical operation of addition and the non-integer concactenator.

What I mean by that long string of words is this: Javascript, when it encounters an expression with a + in it, does its best to guess as to what is expected of it. If both items are numbers (integers, floats, doubles, boolean) it decides that you want to add the two things together. If either one is not a number, then it decides you want to concat the two things together. This isn't always correct, and sometimes it baffles even the best of us.

Your console tests look a little like this in the world of behind-the-scenes javascript.

console.log("1.) expression ", (1 < 2).toString() + 3 > 2);
//gets evaluated to :
//"1.) expression ", "true"+3 > 2)
//= "1.) expression", "true3" > 2) <- can't compare these two logically, so we get false
//= "1.) expression", false <- is finally passed to function
console.log("2.) expression ", (3 < 2).toString(), 3 > 2);
//"2.) expression ", "false" , true
console.log("3.) expression " + (3 > 2).toString() + 3 > 2);
//"3.) expression " + "false" + 3 > 2
//="3.) expression false" + 3 > 2
//="3.) expression false3" > 2 <- same thing, can't logically compare, so false.
console.log("4.) expression ", 3 < 2);
//"4.) expression ", false
console.log("5.) expression " + 3 < 2);
//"5.) expression 3" < 2 <- can't logically compare
//false
console.log("6.) expression " + n + 3 < 2);
//Same thing as #4, but not with an extra variable that gets combined to the string.

I think of this like order of operations, prioritizing grouped expressions first and then going left to right.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM