The functionality of the Number()
method is to convert any data input into an integer. However, if you pass symbols and spaces into it, such as '10,00 €'
, it will return NaN.
To extract only the symbol from a string in JavaScript, there are built-in methods that can be used together to handle this easily.
If the position of the symbol is known, we can retrieve the symbol at that position using the charAt()
method.
function knownLocation(s) {
return s.charAt(s.length - 1)
}
If the position of the symbol is unknown, but its presence is known, we can utilize the .includes
method to check for the symbol and return the character at the index.
function knownValue(s) {
if (s.includes("$")) {
return s.charAt(s.indexOf("$"))
} else if (s.includes("€")) {
return s.charAt(s.indexOf("€"))
} else {
return undefined
}
}