Strings & Numbers Overview
- This lesson introduces string and number data types in JavaScript.
Introduction to Strings & Numbers
In JavaScript, Strings and Numbers are two of the most commonly used data types.
They are used in almost every application:
User input
Form validation
Calculations
Displaying messages
Processing data
Understanding how strings and numbers work is fundamental to mastering JavaScript.
In this lesson, we will cover:
Overview of Strings & Numbers
String Methods
String Immutability
Overview of Strings & Numbers
What is a String ?
A string is a sequence of characters used to represent text.
Strings are written inside:
Single quotes ' '
Double quotes " "
Backticks ` `
Different Ways to Declare Strings in JavaScript
Shows how to create strings using double quotes, single quotes, and template literals
let name = "Rahul";
let city = 'Ahmedabad';
let message = `Welcome to JavaScript`;
All three are valid string declarations.
What Can a String Contain ?
A string can contain:
Letters
Numbers
Symbols
Spaces
What Can Be Stored Inside a String
A string can hold letters, numbers, symbols, and spaces
let info = "Room No. 303";
What is a Number ?
A number represents numeric values used for:
Counting
Calculations
Comparisons
Mathematical operations
Understanding Numbers in JavaScript
Numbers represent numeric values used for calculations and comparisons
let age = 25;
let price = 99.50;
let temperature = -5;
- All are treated as number type.
Checking Data Types Using typeof Operator
Uses typeof to identify the data type of variables
let text = "Hello";
let num = 10;
console.log(typeof text); // string
console.log(typeof num); // number
Strings vs Numbers
Feature String Number Purpose Text data Numeric data Quotes required Yes No Example "Hello" 10 Operations Concatenation Arithmetic
String and Number Addition Confusion
Shows how JavaScript concatenates string and number instead of adding them
let a = "10";
let b = 5;
console.log(a + b); // "105"
Reason: "10" is a string, so JavaScript performs string concatenation, not addition.
String Methods
What Are String Methods ?
String methods are built-in functions that allow us to:
Manipulate text
Search inside strings
Modify string values
Extract parts of a string
Commonly Used String Methods
length
Finding String Length
Returns the total number of characters present in a string
let text = "JavaScript";
console.log(text.length);
- toUpperCase()
Convert String to Uppercase
Changes all characters in a string to uppercase letters
let name = "javascript";
console.log(name.toUpperCase());
- toLowerCase()
Convert String to Lowercase
Changes all characters in a string to lowercase letters
let name = "HELLO";
console.log(name.toLowerCase());
- trim()
Remove Extra Spaces from String
Eliminates spaces from both the start and end of a string
let msg = " Hello World ";
console.log(msg.trim());
- includes()
Check Substring Presence in String
Determines whether a string contains a specific sequence of characters
let text = "Learning JavaScript";
console.log(text.includes("Java"));
- indexOf()
Find Position of Character in String
Returns the index of the first occurrence of a character or substring
let text = "JavaScript";
console.log(text.indexOf("S"));
- slice()
Extract Substring Using Slice Method
Retrieves a portion of a string based on start and end index
let text = "JavaScript";
console.log(text.slice(0, 4));
- replace()
Replace Text Within a String
Substitutes a specified value with another inside a string
let text = "Hello World";
console.log(text.replace("World", "JavaScript"));
String Immutability
What is Immutability ?
Immutability means:
Once a string is created, it cannot be changed.
JavaScript strings are immutable.
Strings Cannot Be Modified Directly
Demonstrates that strings are immutable and cannot be changed by index
let str = "Hello";
str[0] = "Y";
console.log(str); // "Hello"
The string does not change.
Why Does This Happen ?
Because strings in JavaScript:
Cannot be modified character by character
Always create a new string when changed
String Methods Do Not Modify Original Value
Shows that string methods return new values and do not change the original string
// Example: Method Does Not Change Original String
let text = "hello";
text.toUpperCase();
console.log(text);
// Correct Way to Update a String
let text = "hello";
text = text.toUpperCase();
console.log(text);
Why Are Strings Immutable ?
Better performance
Improved memory management
Predictable behavior
Security benefits
String Immutability vs Arrays
Feature String Array Mutable No Yes Can change index No Yes Methods modify original No Some methods do Common Mistakes
Expecting string methods to change original value
Trying to update string characters directly
Forgetting to store returned string
Best Practices
Always assign the result of string methods
Understand immutability clearly
Avoid unnecessary string re-creation
Use meaningful variable names