Confused about quotations


Joined
Mar 23, 2023
Messages
5
Reaction score
0
Hi,
I'm studying JavaScript and this little bunch of code came up. I'm not understanding how the quotation marks work in this. It's suppopsed to label the ellipse with the mouseX and mouseY coordinates, and it works just fine. I'm just hoping for a new perspective that will help me understand what those quotes mean. Thanks for you help,
Kevin


fill(255, 0, 255);

draw = function() {
background(255, 255, 255);
ellipse(mouseX, mouseY, 12, 12);
var label = mouseX+", "+mouseY;
text(label, mouseX, mouseY);
};
 
Ad

Advertisements

Joined
Sep 4, 2022
Messages
71
Reaction score
4
JavaScript:
var label = mouseX+", "+mouseY;

that is a "String build"

you can create String with surrounding " "
JavaScript:
var test_one = "hello " ;
or involve more Strings, and numerics value too

JavaScript:
var country_code = 44 ;
var all = "frogger" + country_code ;
var test_two = "hello " + all + "How are you " + "?" ;


the "," in the code is part of the label to display with 2 numeric parameters around the ","
+ is an concatenating operator to build string, and useful to join all parts of a String.

you have 2 numerics value , and they must be separate by "," character.

so the final String to display with name 'label' is made by :
var label = mouseX + "," + mouseY ;
var label contains the string to output with X and Y coordinates of the Mouse
 
Joined
Mar 23, 2023
Messages
5
Reaction score
0
Looking again at the original code. Why does the text not repeat mouseX and mouseY twice? Since the text is the variable label, which is mouseX and mouseY and then is followed again by mouseX and mouseY?
 
Joined
Sep 4, 2022
Messages
71
Reaction score
4
when you have :
JavaScript:
var label = "you allocate the value IN the var with name 'label'";

JavaScript:
text(parameter_1 => text , parameter_2 => X value ,parameter_3 => Y value ) is a function.
MouseX and MouseY are used to give the 'location' of the label to display.
 
Ad

Advertisements

Joined
Mar 23, 2023
Messages
5
Reaction score
0
Okay, thank you. I think I see. The 'what' and the 'where' which in this example are the same.
 
Ad

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. After that, you can post your question and our members will help you out.

Ask a Question

Top