Class-6
Class 6 – Introduction To JavaScript
Class Slides: Class 6.1
This lecture covers essential JavaScript functions for manipulating strings, performing mathematical operations, and handling date and time. Mastering these functions enhances the ability to create dynamic and interactive web applications.
Key Topics Covered:
1. String Functions in JavaScript:
- toLowerCase() & toUpperCase(): Convert strings to lowercase or uppercase, useful for consistent formatting and comparisons.
- slice(): Extracts a section of a string based on specified indices and returns a new string.
- indexOf() & lastIndexOf(): Find the position of the first or last occurrence of a specified value in a string. Returns
-1if not found. - charAt(): Returns the character at a specified index in a string.
- replace(): Replaces a specified value within a string. Can use regular expressions for case-insensitive or global replacements.
- split(): Splits a string into an array of substrings based on a specified separator (e.g., space, comma, etc.).
2. Math Functions in JavaScript:
- Math.round(): Rounds a number to the nearest integer.
- Math.ceil(): Rounds a number up to the next largest integer.
- Math.floor(): Rounds a number down to the nearest smaller integer.
- Math.random(): Generates a random number between 0 (inclusive) and 1 (exclusive). Can be scaled to generate random numbers within a specific range.
- Additional functions include Math.pow() (exponentiation), Math.sqrt() (square root), Math.abs() (absolute value), Math.min(), Math.max(), and Math.log().
- toFixed(): Limits a number to a specific number of decimal places.
3. Date Object in JavaScript:
- Creating Date Objects:
new Date()creates a date object with the current date and time.- Dates can also be created with specific parameters:
new Date(year, month, day, hours, minutes, seconds, milliseconds).
- Unix Time & Epoch Time:
- Unix Time represents the number of milliseconds since January 1, 1970. Use
getTime()to retrieve this value. - Epoch Time corresponds to
0in Unix time.
- Unix Time represents the number of milliseconds since January 1, 1970. Use
- Retrieving Date Components:
getFullYear(),getMonth(),getDate(),getHours(),getMinutes(),getSeconds(), etc., retrieve specific components of a date.
- Modifying Dates:
- Use
setFullYear(),setMonth(), etc., to modify date components.
- Use
- Converting Day of the Week to Text:
getDay()returns a number representing the day of the week (0 for Sunday, 1 for Monday, etc.). This can be mapped to textual representations using arrays.
- Calculating Time Differences:
- Subtracting two dates using
getTime()gives the time difference in milliseconds. This can be converted to days by dividing by the number of milliseconds in a day.
- Subtracting two dates using
This lecture introduces key string manipulation techniques, mathematical functions, and date handling methods in JavaScript. These skills are fundamental for developing feature-rich applications that require text formatting, mathematical calculations, and date-time operations.
