34

How would I do something like this in javascript:

var s = '124sdg'; var f = 'hsdsda'; if (s.startsWith(a number)) { // Return true } if (f.startsWith(a number)) { // Return false } 
0

2 Answers 2

43

You can use a regular expression:

if (s.match(/^\d/)) { // Return true } if (f.match(/^\d/)) { // Return false } 
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is overrated. If we only need to check the regex against the value, .test performs better. Please refer here.
Yes, agreed, test is better here.
28

use a regex...

/^\d/.test(s) 

which will return true or false if the first digit is a number or not

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.