Join is not a function
Asked Answered
H

2

18

I'm playing around with PEG.js

start = keyword

keyword = a:[a-z]? {return a.join("");}

Why am I getting here the error:

a.join is not a function

when I enter a valid string like abc?

Hostess answered 31/10, 2015 at 11:30 Comment(0)
C
24

join() is an array-function, you cannot use it on a string: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Candlelight answered 31/10, 2015 at 12:42 Comment(1)
[a-z]? will produce either a string containing one character or a null. If you used [a-z]* you would get an array that you could call join on. However, if you used $[a-z]*, that join would be done for you automatically, yielding a string with zero or more characters in it.Reminiscence
L
0

I was facing this error when I was applying join function and then returning the result in the same string array variable, we cannot do that we should save the result in some other string type variable

Launcher answered 25/11, 2021 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.