Hello I am trying to implement automated test scripts using NewRelic. I am engineering javascript such that I print the last character of a field on the page that is always 8 characters. I have this:
var last_character=page_value.charAt(8);
console.log('test last_character='+last_character)
but it only prints 'test last_character='! Is this a javascript variable scoping issue? I have never understood that!
1 Answer 1
I think you want to use .charAt(7)
since js starts indexing at position 0.
answered Apr 25, 2017 at 18:15
-
Thank you! This was the problem. I wish javascript gave an error rather than an un-usable value!SrGuy– SrGuy2017年04月25日 19:54:42 +00:00Commented Apr 25, 2017 at 19:54
-
@SrGuy Since you know it's an 8 character string, you can do this. You may want to do str.charAt(str.length - 1) in the future in case the string changes on you.2017年04月25日 21:40:15 +00:00Commented Apr 25, 2017 at 21:40
-
string.slice(-1) is a better approach, since it is cross-browser.João Farias– João Farias2017年04月26日 12:39:24 +00:00Commented Apr 26, 2017 at 12:39
charAt(7)