Lesson: Introduction to JavaScript – Data Types
Objective
By the end of this lesson, students will:
- Understand the four main primitive data types in JavaScript.
- Differentiate between strings, numbers, booleans, and null values.
- Write and debug code using these data types effectively.
Introduction
In JavaScript, data types are the foundation of programming. They define the kind of values a variable can hold. JavaScript includes primitive data types, which are the simplest and most fundamental forms of data.
The four primary primitive data types in JavaScript are:
- Strings
- Numbers
- Booleans
- Null
Explanation of Primitive Data Types
1. Strings
Strings are used to represent text. They are sequences of characters wrapped in single (') or double (") quotes.
Example:
2. Numbers
Numbers represent numerical values, including both integers and decimals.
Example:
3. Booleans
Booleans represent logical states and can only be true or false.
Example:
4. Null
Null is a special keyword that represents the intentional absence of any value.
Example:
Code Example
The following code demonstrates all four primitive data types:
Exercise 1: Write Your Own Examples
Instructions
Write a program that:
- Logs a string of your choice.
- Logs a number of your choice.
- Logs a boolean value.
- Logs a null value.
Solution
Exercise 2: Debugging Activity
Here’s a buggy code snippet. Identify and fix the errors:
Solution
Exercise 3: Advanced Challenge
- Create variables for each primitive data type and assign them values.
- Log the type of each variable using the
typeofoperator.
Solution
Quiz
Multiple Choice Questions
- Which of the following is a string?
- a)
"Hello" - b)
42 - c)
true - d)
null
Answer: a)
- a)
- What does
nullrepresent in JavaScript?- a) A string with no characters.
- b) A value that equals zero.
- c) The intentional absence of any value.
- d) A boolean value.
Answer: c)
- What will
typeof nullreturn in JavaScript?- a)
null - b)
undefined - c)
object - d)
boolean
Answer: c)
- a)
Coding Quiz
Write a program to:
- Declare a variable for each primitive data type.
- Log each variable and its type using
typeof.
SOLUTION
Recap
- Strings are sequences of characters wrapped in quotes.
- Numbers represent numerical values.
- Booleans are logical values (
trueorfalse). - Null represents the absence of value.
Related Videos:
Related Posts:
Using CSS classes and the class attribute
Storing and Modifying Information (CRUD)
The Python Standard Library Ver 3.13.1
Why is Java not a pure object oriented language?
Introduction to JavaScript – Create a Variable: let
Learn about JavaScript ELSE STATEMENTS
