0

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!

2
  • 1
    There is a discrepancy between your nick (user name) and your skill level :-) Apr 25, 2017 at 19:51
  • This is a really basic question: javascript starts counting the index at 0 not 1. So you need charAt(7)
    – Kate Paulk
    Apr 26, 2017 at 11:24

1 Answer 1

0

I think you want to use .charAt(7) since js starts indexing at position 0.

3
  • Thank you! This was the problem. I wish javascript gave an error rather than an un-usable value!
    – SrGuy
    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.
    – corsiKa
    Apr 25, 2017 at 21:40
  • string.slice(-1) is a better approach, since it is cross-browser. Apr 26, 2017 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.