Q
How do you get the index of the first occurrence of a specified substring in a string?

Answer & Solution

Answer: Option A
Solution:
The .indexOf(substring) method in JavaScript returns the index within the calling string object of the first occurrence of the specified substring.
Related Questions on Average

What does the .toLowerCase() method do in JavaScript?

A). Converts all characters to lowercase

B). Converts all characters to uppercase

C). Trims leading and trailing whitespace

D). Converts string to a boolean

Which method can you use to check if a string includes a specific substring in JavaScript?

A). .includes()

B). .contains()

C). .hasSubstring()

D). .indexOf()

What does the + operator do with strings?

A). Concatenates strings

B). Compares strings

C). Trims strings

D). Converts strings to uppercase

What is the result of 'Hello'.substring(0, 3) in JavaScript?

A). 'Hell'

B). 'Hello'

C). 'o'

D). 'llo'

How do you concatenate two strings in JavaScript?

A). string1.concat(string2)

B). string1 + string2

C). string1.join(string2)

D). string1.combine(string2)

How do you trim whitespace from the beginning and end of a string in JavaScript?

A). string.trim()

B). trimString(string)

C). string.strip()

D). trim(string)

What is the result of 'hello'.toUpperCase().toLowerCase() in JavaScript?

A). 'hello'

B). 'HELLO'

C). 'Hello'

D). 'hello'

What is the correct way to compare two strings in JavaScript for equality?

A). string1 == string2

B). string1.compare(string2)

C). string1.equals(string2)

D). string1 === string2

What does the .replace() method do in JavaScript?

A). Replaces a specified value with another value

B). Removes a substring

C). Splits a string into an array

D). Joins two or more strings

What is the result of '5' + 3 in JavaScript?

A). '53'

B). 8

C). 53

D). '5 3'