avatar
Raja MS
0

Codes

9

Answers

Code compilers

Top answers

0
How to Check if a JavaScript array contains a string​
February-14-2024
let colors = ["Red", "Green", "Blue"];
console.log(colors.indexOf("Green"));  // Output: 1
console.log(colors.indexOf("White"));  // Output: -1
0
Compare dates without time using date strings
February-02-2024
let date1 = new Date("2024-01-29T03:34:48.000Z"); // Tue Jan 29 2024 03:34:48 GMT+0000
let date2 = new Date("2024-01-29T15:00:00.000Z"); // Tue Jan 29 2024 15:00:00 GMT+0000
let date1String = date1.toDateString();           // "Tue Jan 29 2024"
let date2String = date2.toDateString();           // "Tue Jan 29 2024"
console.log(date1String === date2String); // Output: true
console.log(date1String < date2String);   // Output: false
console.log(date1String > date2String);   // Output: false
0
JavaScript remove character from string
January-28-2024
const originalString = "Hello, World!";
const removedCharacter = originalString.substring(0, 5) + " " + originalString.substring(7);
console.log(removedCharacter);
// Output: "Hello World!"
0
JavaScript format number with commas using toLocaleString()
January-22-2024
const number = 1234567.89;
const formattedNumber = number.toLocaleString(); 
console.log(formattedNumber)
// Output: "1,234,567.89"
0
JavaScript substring() vs slice()
January-13-2024
const greet = "Hello, World!";

console.log(greet.substring(0, 5)); 
// Output: "Hello"

console.log(greet.slice(0, 5));     
// Output: "Hello"
0
How to use JavaScript substring() to extract substring
January-13-2024
const greet = "Hello, World!";
const substring = greet.substring(0, 5);
console.log(substring); 
// Output: "Hello"
0
Time zones and daylight saving time (DST)
January-12-2024
const now = new Date();
console.log(now.getTimezoneOffset()); 
// Output: -330 (UTC+05:30)
0
JavaScript Date methods
January-12-2024
const date = new Date();

console.log(date.getFullYear()); // Output: 2023
console.log(date.getMonth()); // Output: 2 (March is month 2)
console.log(date.getDate()); // Output: 25
console.log(date.getDay()); // Output: 6 (Saturday is day 6)
console.log(date.getHours()); // Output: 11
console.log(date.getMinutes()); // Output: 50
console.log(date.getSeconds()); // Output: 31
console.log(date.getMilliseconds()); // Output: 553
console.log(date.getTime()); 
// Output: 1679722523432 (time in milliseconds since January 1, 1970)
0
Creating a Date object in JavaScript
January-12-2024
// #1. Create a Date object with the current date and time
let currentDate = new Date();

// #2. Create a Date object with a specific date and time
//  (year, month, day, hour, minute, second)
let specificDate = new Date(2024, 0, 8, 2, 50, 2); 

// #3. Create a Date object from a string
let stringDate = new Date("January 8, 2024 02:50:02");

// #4. Create a Date object from a timestamp
// ( milliseconds since January 1, 1970)
let timestampDate = new Date(1700000000000);