Name | Progress |
---|---|
JavaScript for Beginners Introduction | Not Read |
Setting up environment | Not Read |
Name | Progress |
---|---|
Section 1 | Not Read |
Section 2 | Not Read |
Section 3 | Not Read |
Module 1 Quiz | Not Attempted |
Name | Progress |
---|---|
Section 1 | Not Attempted |
Section 2 | Not Attempted |
This lesson will introduce you to different types of variables in JavaScript, show you how to convert between these types, and teach you how to use the typeof operator to identify a variable's type. These concepts are essential for handling data correctly in your JavaScript programs.
Data Types
Variables in JavaScript can hold different types of values, such as numbers, strings, and booleans. Here are some examples:
Type Conversion
Type conversion in JavaScript refers to changing data from one type to another, such as turning a string into a number or a number into a string.
String conversion
let a = 0;
a = String(a);
console.log(a); // Outputs: '0'
let b = true;
b = String(b);
console.log(b); // Outputs: 'true'
Explanation: Converting different types to strings is straightforward with the String() method. Numbers and booleans when converted to strings retain their literal values in string format. This is useful for logging, concatenation with other strings, or displaying values to users.
Number conversion
let a = '0';
a = Number(a);
console.log(a); // Outputs: 0
let b = false;
b = Number(b);
console.log(b); // Outputs: 0
let c = true;
c = Number(c);
console.log(c); // Outputs: 1
Explanation: Using the Number() method allows the conversion of strings and booleans to numbers. The string '0' converts to the numeric 0. Boolean false converts to 0, and true converts to 1. This is crucial for calculations and logical operations where numeric values are required.
Boolean conversion
let a = '0';
a = Boolean(a);
console.log(a); // Outputs: true
let b = 0;
b = Boolean(b);
console.log(b); // Outputs: false
Explanation: The Boolean() conversion is influenced by values being "truthy" or "falsy." Non-empty strings like '0' are truthy and convert to true. The number 0 is a falsy value and converts to false. Understanding this helps manage conditional statements and control flows effectively.
Using <strong>typeof</strong> operator
typeof is a unary operator that returns a string indicating the type of the unevaluated operand. It's simple to use and helps in checking the data type of a variable, which is crucial for functions that require type-specific operations.
Strings
let name = "Alice";
console.log(typeof name); // Outputs: 'string'
Numbers
let age = 30;
console.log(typeof age); // Outputs: 'number'
Booleans
let isActive = true;
console.log(typeof isActive); // Outputs:'boolean'
Objects
let user = {name: "Alice", age: 30};
console.log(typeof user); // Outputs: 'object'
let numbers = [1, 2, 3];
console.log(typeof numbers); // Outputs: 'object'
let nothing = null;
console.log(typeof nothing); // Outputs: 'object'
Functions
function display() {
console.log("Hello!");
}
console.log(typeof display); // Outputs:'function'
Undefined
let something;
console.log(typeof something); // Outputs: 'undefined'