w3resource

JavaScript: Check whether the given argument is a string

JavaScript fundamental (ES6 Syntax): Exercise-187 with Solution

Check If String

Write a JavaScript program to check whether the given argument is a string.

  • Use typeof to check if a value is classified as a string primitive.

Sample Solution:

JavaScript Code:

// Define a function 'isString' that checks if the type of value 'val' is 'string'
const isString = val => typeof val === 'string';

// Test case to check if the provided value is a string
console.log(isString('10')); // true ('10' is a string)

Output:

true

Visual Presentation:

JavaScript Fundamental: Check whether the given argument is a string.
JavaScript Fundamental: Check whether the given argument is a string.

Flowchart:

flowchart: Check if the given argument is a string

Live Demo:

See the Pen javascript-basic-exercise-187-1 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript program that returns true if the provided value is a string using typeof and Object.prototype.toString.
  • Write a JavaScript function that checks whether an argument is a string and distinguishes it from String objects.
  • Write a JavaScript program that validates a variable’s type and confirms it is a string literal.
  • Write a JavaScript function that tests an input with multiple type-check methods to determine if it is a string.

Go to:


PREV : Check If Symbol.
NEXT : Array Sort Order.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.