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
namestores the text"Aman".- Text written inside quotes is a String.
typeof namechecks the data type of the variable.- 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
agestores the value15.15is a numeric value, so JavaScript treats it as a Number.typeof agechecks the data type.- 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
isStudentstorestrue.trueis a Boolean value.- Boolean values can be either
trueorfalse. typeof isStudentreturns"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
- The variable
cityis declared usinglet. - No value is assigned to it.
- JavaScript automatically gives it the value
undefined. - 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
nullrepresents an intentional absence of a value.- We explicitly assign
nulltophoneNumber. console.log(phoneNumber)displaysnull.- An important JavaScript behavior is that
typeof nullreturns"object". - 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
- Curly braces
{}are used to create an object. - The object contains two properties:
nameandage. namestores a string.agestores a number.typeof studentreturns"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
- Square brackets
[]are used to create an array. - The array contains three string values.
console.log(subjects)displays all the values.- JavaScript technically treats arrays as objects.
- Therefore,
typeof subjectsreturns"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
namecontains text, so its type isstring.agecontains a number, so its type isnumber.isStudentcontainstrue, so its type isboolean.typeofhelps 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
"100"is written inside quotation marks, so it is a string.100is written without quotation marks, so it is a number.- Both values look similar, but JavaScript treats them as different data types.
typeofhelps 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
namecontains text, so its type isstring.agecontains a number, so its type isnumber.isStudentcontainstrue, so its type isboolean.cityhas no assigned value, so its type isundefined.phonecontainsnull. JavaScript reports its type asobject.studentis an object, so its type isobject.marksis an array, and JavaScript reports arrays asobjectwhen usingtypeof.- For a specific array check, use
Array.isArray().
Key Takeaways
- A data type tells JavaScript what kind of value is being stored.
stringis used for text.numberis used for numeric values.booleancontains eithertrueorfalse.undefinedusually means a variable has been declared but has not been given a value.nullrepresents an intentional absence of a value.- Objects store data using properties and values.
- Arrays are used to store collections of values.
typeofis used to check the data type of a value.typeof nullreturns"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.
