String Object
The String object in JavaScript provides various methods for manipulating text strings. Here are some commonly used properties and methods:
// length property
const text1 = "helloWorld";
document.write(text1.length);
document.write("<br></br>");
// bold(): makes text bold
const text2 = "importantText";
document.write(text2.bold());
document.write("<br></br>");
// fontcolor(): sets text color
const text3 = "warningMessage";
document.write(text3.fontcolor("red"));
document.write("<br></br>");
// fontsize(): sets font size
const text4 = "largeText";
document.write(text4.fontsize(7));
document.write("<br></br>");
// link(): displays string as hyperlink
const text5 = "clickHere";
document.write(text5.link("../html/img1.html"));
document.write("<br></br>");
// sub() and sup() methods: subscript and superscript
const base1 = "2";
const base2 = "100";
const exponent = "3";
document.write(base1.sub());
document.write(base2);
document.write(exponent.sup());
document.write("<br></br>");
// concat(): string concatenation
const part1 = "hello";
const part2 = "world";
document.write(part1.concat(part2));
document.write("<br></br>");
// charAt(): returns character at specified position
const text6 = "javascript";
document.write(text6.charAt(3));
document.write("<br></br>");
// indexOf(): returns position of substring
const text7 = "programming";
document.write(text7.indexOf("g"));
document.write("<br></br>");
// split(): splits string into array
const text8 = "apple,banana,orange";
const fruits = text8.split(",");
for(let i=0; i<fruits.length document.write="" i="">");
// replace(): replaces substring
const text9 = "oldValue";
document.write(text9.replace("old", "new"));
document.write("<br></br>");
// substr() and substring(): extracts substring
const text10 = "substringExample";
document.write(text10.substr(1,5)); // starts at position 1, extracts 5 characters
document.write("<br></br>");
document.write(text10.substring(1,5)); // from position 1 to 4 (exclusive)
</fruits.length>
Array Object
The Array object provides various methods for working with arrays:
Properties
- length: represents the array length
Common Methods
- concat(): merges two arrays
- join(): joins array elements with a separator
- push(): adds element to end of array and returns new length
- pop(): removes last element and returns it
- reverse(): reverses array order
- sort(): sorts array elements
- toString(): converts array to string
// concat() example
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const merged = array1.concat(array2);
document.write(merged);
// join() example
const colors = ["red", "green", "blue"];
const colorString = colors.join("-");
document.write(colorString);
// push() and pop() example
const numbers = [10, 20, 30];
numbers.push(40);
document.write(numbers.pop());
// reverse() example
const letters = ["a", "b", "c"];
letters.reverse();
document.write(letters);
// sort() example
const values = [3, 1, 4, 2];
values.sort();
document.write(values);
// toString() example
const mixed = [1, "two", 3];
const stringVersion = mixed.toString();
document.write(stringVersion);
Date Object
The Date object provides methods for working with dates and times:
Creating Date Objects
const currentDate = new Date();
Common Methods
| Method | Description |
|---|---|
| Date() | Returns current date and time |
| getDate() | Returns day of month (1-31) |
| getDay() | Returns day of week (0-6) |
| getMonth() | Returns month (0-11) |
| getFullYear() | Returns four-digit year |
| getHours() | Returns hours (0-23) |
| getMinutes() | Returns minutes (0-59) |
| getSeconds() | Returns seconds (0-59) |
| getTime() | Returns milliseconds since Jan 1, 1970 |
| toString() | Converts Date object to string |
| toLocaleString() | Converts to local time string |
// Date object examples
const now = new Date();
document.write(now.toString());
const specificDate = new Date(2023, 0, 15); // January 15, 2023
document.write(specificDate.toLocaleDateString());
const hours = now.getHours();
const minutes = now.getMinutes();
document.write(`Current time: ${hours}:${minutes}`);
Math Object
The Math object provides mathematical constents and functions. All methods are static and caled using Math.method().
// Common Math methods
document.write(Math.PI); // Returns π
document.write(Math.E); // Returns Euler's number
// Rounding methods
document.write(Math.round(4.7)); // Rounds to nearest integer
document.write(Math.ceil(4.3)); // Rounds up
document.write(Math.floor(4.9)); // Rounds down
// Power and root
document.write(Math.pow(2, 3)); // 2 to the power of 3
document.write(Math.sqrt(16)); // Square root
// Trigonometry
document.write(Math.sin(0)); // Sine
document.write(Math.cos(Math.PI)); // Cosine
// Random number
document.write(Math.random()); // Returns random number between 0 and 1
// Minimum and maximum
document.write(Math.min(5, 10, 2)); // Returns smallest value
document.write(Math.max(5, 10, 2)); // Returns largest value