srb633
srb633

Reputation: 813

Call specific word in string by index number?

I've been learning javascript for a solid day lol. Wondering how I can call a specific word in a string by it's count/index. An index would be desirable because the string is user input, so it could be anything.

I tried

const User_Input = "Some Text"
console.log(User_Input[1])

But that of course calls the index by letter. So the outcome was

"o"

The desired output is

"Text"

Upvotes: 0

Views: 65

Answers (2)

Satyam Pathak
Satyam Pathak

Reputation: 6912

let User_Input = "Some Text"
User_Input = User_Input.split(' ')
console.log(User_Input[1])

Upvotes: 2

Dane Brouwer
Dane Brouwer

Reputation: 2972

Very simple to achieve using split

const User_Input = "Some Text";

const split = User_Input.split(" ");

console.log(split);
console.log(split[1]);

Upvotes: 3

Related Questions