🔧 The String Data Type in JavaScript
In JavaScript, the String data type is used to represent a sequence of characters enclosed within quotes. Strings can be enclosed in single quotes (' '
), double quotes (" "
), or backticks (` `
) for template literals.
📘 Example:
let message = "Hello, JavaScript!";
Output:
console.log(message); // Output: Hello, JavaScript!
🔑 Common String Methods in JavaScript
1. length
The length
property returns the number of characters in a string.
let message = "Hello";
console.log(message.length); // Output: 5
2. toUpperCase()
and toLowerCase()
The toUpperCase()
method converts all characters of a string to uppercase, while toLowerCase()
converts them to lowercase.
let text = "JavaScript";
console.log(text.toUpperCase()); // Output: JAVASCRIPT
console.log(text.toLowerCase()); // Output: javascript
3. charAt()
The charAt()
method returns the character at a specified index in the string.
let word = "Hello";
console.log(word.charAt(1)); // Output: e
4. indexOf()
The indexOf()
method returns the first index at which a specified character or substring is found. If the character is not found, it returns -1
.
let sentence = "Welcome to JavaScript!";
console.log(sentence.indexOf("JavaScript")); // Output: 11
5. substring()
The substring()
method returns a part of the string between two specified indices.
let text = "JavaScript";
console.log(text.substring(0, 4)); // Output: Java
📐 Introduction to JavaScript Functions
🔧 Math Functions in JavaScript
The Math object in JavaScript is a built-in object that provides mathematical operations such as calculations, rounding, and generating random numbers.
1. Math.random()
Generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
let randomNum = Math.random();
console.log(randomNum); // Output: Random number between 0 and 1
2. Math.round()
The Math.round()
method rounds a number to the nearest integer.
let roundedValue = Math.round(3.6);
console.log(roundedValue); // Output: 4
3. Math.floor()
The Math.floor()
method rounds a number down to the nearest integer.
let floorValue = Math.floor(3.7);
console.log(floorValue); // Output: 3
4. Math.ceil()
The Math.ceil()
method rounds a number up to the nearest integer.
let ceilValue = Math.ceil(3.3);
console.log(ceilValue); // Output: 4
5. Math.max()
and Math.min()
Returns the largest and smallest values from a list of numbers, respectively.
let max = Math.max(5, 10, 15);
let min = Math.min(5, 10, 15);
console.log(max); // Output: 15
console.log(min); // Output: 5
📅 Date Functions in JavaScript
The Date object is used to handle dates and times in JavaScript. It provides various methods for working with dates and times, including getting the current date, setting a specific date, and formatting dates.
1. new Date()
Creates a new date object representing the current date and time.
let today = new Date();
console.log(today); // Output: Current date and time
2. getFullYear()
Returns the full year (4 digits) of the specified date.
let date = new Date();
console.log(date.getFullYear()); // Output: Current year
3. getMonth()
Returns the month (0-11) of the specified date, where 0 is January and 11 is December.
let date = new Date();
console.log(date.getMonth()); // Output: Current month (0-11)
4. getDate()
Returns the day of the month (1-31) of the specified date.
let date = new Date();
console.log(date.getDate()); // Output: Current day of the month
5. toLocaleDateString()
Returns a string representing the date portion of a Date object, formatted according to the locale.
let date = new Date();
console.log(date.toLocaleDateString()); // Output: Local formatted date
📌 Conclusion
Strings, Math functions, and Date functions are essential components of JavaScript. The string methods provide various tools for manipulating text, while Math functions help in performing mathematical operations. Date functions allow developers to handle and format dates and times efficiently.