-1

I don't work with JavaScript, but I'm trying to find this answer. We want to change the _ within variable names into space. I know JavaScript does not allow this, but is there a clever way of doing this like concatenating the two into one such as:

var firstname + ' ' + lastname

Then put

firstname lastname = "Jack Jill";

Or perhaps some library calls. I know the above does not work because of + and '

Manuel Abascal
  • 5,616
  • 5
  • 35
  • 68
kevbuntu
  • 461
  • 1
  • 12
  • 27
  • 1
    `let [firstname, lastname] = "Jack Jill".split(" ");` – ASDFGerte Oct 18 '19 at 17:57
  • Can I ask why you want to do this? – Brett East Oct 18 '19 at 17:57
  • 1
    I'm having some difficulty understanding the question. Do you want a variable that contains a concatenation of two strings, eg `firstName = "Jack"; lastName = "Jill"; fullName=firstName+lastName;` or are you trying to declare a variable called `firstName lastName`? If the latter, you cannot do that (to my knowledge) in javascript. Whitespace is not valid in identifiers. Probably just use `snakeCase`, eg `firstNameLastName` – CollinD Oct 18 '19 at 17:58
  • It is a whole set of column names that the client wants changed, their preference – kevbuntu Oct 18 '19 at 17:58
  • 1
    I doubt your client cares if your variable names use a space or an underscore... – Gershom Maes Oct 18 '19 at 17:59
  • @CollinD yes is the later that I want to do and javascript does not allow. Wonder if anyone may have come up with a clever hack – kevbuntu Oct 18 '19 at 18:00
  • Exactly, the variable name itself shouldn't really matter only the value – Brett East Oct 18 '19 at 18:00
  • 2
    I suspect that what you actually want is not what you're asking for, or what the client actually wants. Can you describe the problem a little better? – Brett East Oct 18 '19 at 18:03
  • @kevbuntu If there is a "hack" it would be transpilation, which then takes this out of the realm of javascript. Your question, as asked, has a concrete answer. Javascript does not allow identifiers of that nature. – CollinD Oct 18 '19 at 19:02
  • @CollinD oh well, unfortunately not :) – Jonas Wilms Oct 18 '19 at 19:17
  • Possible duplicate of [What characters are valid for JavaScript variable names?](https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) – CollinD Oct 19 '19 at 01:45

5 Answers5

0

As you need variable name to be spaced not value. You can add it to context e.g.

context is window/this/xyz

window[firstname+' '+lastname] = "Jack Jill";
this[firstname+' '+lastname] = "Jack Jill";
xyz[firstname+' '+lastname] = "Jack Jill";

But while consuming it you have to use in similar(above) faison you can't use them directly as you can consume other variables.

Akhilesh Kumar
  • 9,085
  • 13
  • 57
  • 95
0

You could do something as follows:

var firstName = "Jack";
var lastName = "Jill";

var fullName = firstName + " " + lastName;

console.log(fullName);
BLAKE
  • 149
  • 1
  • 15
0

Spaces themselves are not a valid character for variable names, you should use underscores instead.

If you are looking to add a space between some variables you can do it by combining variables as shown below:

//Declare 2 variables
var firstName = 'kev';
var lastName = 'buntu';

//Declare variable to combine the two and add a space
var fullName = firstName + ' ' + lastName;

//Print the combined variable to verify the result
//should display "kev buntu"
console.log(fullName) 

edit: given the OP's answers in comments it appears that this may be an XY problem, so potentially the solution is just creating a set of display text variables rather than directly using internal variable names as display values.

disc_code22
  • 109
  • 3
0

To strictly answer your question, 'no', there is no way to make a variable name with a space in it. This is not valid javascript.

You can make an object key with a space in it, by wrapping it in quotes, but I would avoid that if possible, and I don't think that's what you want anyway.

let obj = {
  "firstname lastname": "John Doe"
}

You should stick with camel_case or snakeCase and furthermore, your variable names shouldn't be surfaced to the user (or client), so this shouldn't really matter.

Brett East
  • 4,022
  • 2
  • 20
  • 31
0

For sure that's possible:

Using other whitespace than space, here U+3164

let firstNameᅠsecondName = "You See";
console.log(firstNameᅠsecondName);

There are multiple whitespaces in Unicode, just one of them is used by JS to separate identifiers. But please, don't do this.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Is firstName secondName the name of one variable now? can I do firstName SecondName="I see". I am trying an online js editor but nothing is printed to it – kevbuntu Oct 18 '19 at 19:12
  • Please answer the comments of Brett East under your question. – Jonas Wilms Oct 18 '19 at 19:18