JavaScript Data Types Practice Questions with Solutions

Introductions

JavaScript data types tell us what kind of value a variable contains. Understanding data types is important because JavaScript treats text, numbers, true/false values, empty values, and objects differently. In this chapter, you will practice common JavaScript data types such as String, Number, Boolean, Undefined, Null, Object, and Array. The examples start with simple identification tasks and gradually move toward practical usage. JavaScript Data Types practice questions with solutions help to understand the concepts.

Question 1: Identify a String Value

Problem

Create a variable named name and store the text "Aman" in it. Display the value and check its data type using typeof.

Solution

letname="Aman";console.log(name);console.log(typeofname);

Output

Amanstring

Step-by-step Explanation

  1. name stores the text "Aman".
  2. Text written inside quotes is a String.
  3. typeof name checks the data type of the variable.
  4. JavaScript returns "string".

Question 2: Identify a Number

Problem

Create a variable named age and store the number 15 in it. Display the value and its data type.

Solution

letage=15;console.log(age);console.log(typeofage);

Output

15number

Step-by-step Explanation

  1. age stores the value 15.
  2. 15 is a numeric value, so JavaScript treats it as a Number.
  3. typeof age checks the data type.
  4. JavaScript returns "number".

Question 3: Check a Boolean Value

Problem

Create a variable named isStudent and store true in it. Check its data type.

Solution

letisStudent=true;console.log(isStudent);console.log(typeofisStudent);

Output

trueboolean

Step-by-step Explanation

  1. isStudent stores true.
  2. true is a Boolean value.
  3. Boolean values can be either true or false.
  4. typeof isStudent returns "boolean".

Question 4: Understand Undefined

Problem

Create a variable named city without assigning a value to it. Display the variable and check its data type.

Solution

letcity;console.log(city);console.log(typeofcity);

Output

undefinedundefined

Step-by-step Explanation

  1. The variable city is declared using let.
  2. No value is assigned to it.
  3. JavaScript automatically gives it the value undefined.
  4. Its data type is also reported as "undefined".

Question 5: Understand Null

Problem

Create a variable named phoneNumber and intentionally give it the value null. Display the value and check its type.

Solution

letphoneNumber=null;console.log(phoneNumber);console.log(typeofphoneNumber);

Output

nullobject

Step-by-step Explanation

  1. null represents an intentional absence of a value.
  2. We explicitly assign null to phoneNumber.
  3. console.log(phoneNumber) displays null.
  4. An important JavaScript behavior is that typeof null returns "object".
  5. This is a long-standing behavior of JavaScript.

Question 6: Identify an Object

Problem

Create an object containing a student’s name and age. Display the object and check its data type.

Solution

letstudent= {    name: "Rahul",    age: 16};console.log(student);console.log(typeofstudent);

Output

{ name: 'Rahul', age: 16 }object

Step-by-step Explanation

  1. Curly braces {} are used to create an object.
  2. The object contains two properties: name and age.
  3. name stores a string.
  4. age stores a number.
  5. typeof student returns "object".

Question 7: Identify an Array

Problem

Create an array containing three subjects and check its data type using typeof.

Solution

letsubjects= ["English", "Math", "Science"];console.log(subjects);console.log(typeofsubjects);

Output

[ 'English', 'Math', 'Science' ]object

Step-by-step Explanation

  1. Square brackets [] are used to create an array.
  2. The array contains three string values.
  3. console.log(subjects) displays all the values.
  4. JavaScript technically treats arrays as objects.
  5. Therefore, typeof subjects returns "object".

To specifically check whether a value is an array, use:

console.log(Array.isArray(subjects));

Output:

true

Question 8: Identify Multiple Data Types

Problem

Create variables containing a name, age, and student status. Display the data type of each variable.

Solution

letname="Priya";letage=14;letisStudent=true;console.log(typeofname);console.log(typeofage);console.log(typeofisStudent);

Output

stringnumberboolean

Step-by-step Explanation

  1. name contains text, so its type is string.
  2. age contains a number, so its type is number.
  3. isStudent contains true, so its type is boolean.
  4. typeof helps you identify the type of a value.

Question 9: Compare String and Number

Problem

Create two variables: one containing "100" and another containing 100. Check the data type of both values.

Solution

lettextValue="100";letnumberValue=100;console.log(typeoftextValue);console.log(typeofnumberValue);

Output

stringnumber

Step-by-step Explanation

  1. "100" is written inside quotation marks, so it is a string.
  2. 100 is written without quotation marks, so it is a number.
  3. Both values look similar, but JavaScript treats them as different data types.
  4. typeof helps us identify the difference.

This difference becomes especially important when performing calculations.


Question 10: Check Different Data Types Together

Problem

Create variables containing a string, number, Boolean, undefined value, null value, object, and array. Display the data type of each.

Solution

letname="Aman";letage=15;letisStudent=true;letcity;letphone=null;letstudent= {    course: "JavaScript"};letmarks= [80, 90, 85];console.log(typeofname);console.log(typeofage);console.log(typeofisStudent);console.log(typeofcity);console.log(typeofphone);console.log(typeofstudent);console.log(typeofmarks);

Output

stringnumberbooleanundefinedobjectobjectobject

Step-by-step Explanation

  1. name contains text, so its type is string.
  2. age contains a number, so its type is number.
  3. isStudent contains true, so its type is boolean.
  4. city has no assigned value, so its type is undefined.
  5. phone contains null. JavaScript reports its type as object.
  6. student is an object, so its type is object.
  7. marks is an array, and JavaScript reports arrays as object when using typeof.
  8. For a specific array check, use Array.isArray().

Key Takeaways

  • A data type tells JavaScript what kind of value is being stored.
  • string is used for text.
  • number is used for numeric values.
  • boolean contains either true or false.
  • undefined usually means a variable has been declared but has not been given a value.
  • null represents an intentional absence of a value.
  • Objects store data using properties and values.
  • Arrays are used to store collections of values.
  • typeof is used to check the data type of a value.
  • typeof null returns "object" because of a historical JavaScript behavior.
  • typeof [] also returns "object".
  • Array.isArray() is a better way to specifically check whether a value is an array.

FAQs

1. What are data types in JavaScript?

Data types define the kind of value stored in JavaScript, such as a string, number, Boolean, object, or undefined value.

2. How can I check the data type of a value?

You can use the typeof operator.

letage=15;console.log(typeofage);

Output:

number

3. What is a String in JavaScript?

A String represents text and is usually written inside single quotes, double quotes, or backticks.

letname="Aman";

4. What is a Number in JavaScript?

A Number represents numeric values such as 10, 25.5, or -50.

letprice=99.99;

5. What is a Boolean in JavaScript?

A Boolean represents one of two values: true or false.

letisLoggedIn=true;

6. What is the difference between undefined and null?

undefined commonly means that a variable has not been assigned a value, while null is an explicitly assigned value that represents the absence of a value.

7. Why does typeof null return object?

typeof null returning "object" is a historical behavior of JavaScript. Although null represents an intentional absence of a value, the typeof operator reports it as "object".

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top