-2

I want to accomplish the following:

var a = "hello"
var b 
b[0]=a[0] 

so that

  b[0] 
 >"h"

the first letter of b should be the same as the first letter of a so I want to assign a[0] (which is h) to b[0]. I don't want to be restricted to the first letter, I want to use it to do things like

for(var i = 0; i<a; i++){
if( a.match(/[a-z]/i) !==null ){b[i] = " "}
else{b[i] = a[i]}

(replacing all the non-letters with spaces.) (this is just an example since I've been asked to clarify the question)

cubefox
  • 1,251
  • 14
  • 29

1 Answers1

4

You could desturct the string and take an object for changing a letter at a certain position and join it back to a string. Then assign it to the variable.

var a = "hello",
    b = "foo";

b = Object.assign([...b], { 0: a[0] }).join('');

console.log(b);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392