**Creating Date objects**
```javascript
new Date() // current date and time
new Date('2024-03-15') // from ISO string (parsed as UTC midnight)
new Date(2024, 2, 15) // year, month (0-indexed!), day — March 15
new Date(2024, 2, 15, 10, 30) // year, month, day, hour, minute
Date.now() // current timestamp in ms (no 'new' needed)
```
**The months-are-zero-indexed gotcha**
January = 0, December = 11. This trips up almost everyone:
```javascript
new Date(2024, 0, 1) // January 1, 2024 — not February
new Date(2024, 11, 31) // December 31, 2024
// Safer: always parse from ISO string to avoid the index trap
new Date('2024-03-15') // unambiguous
```
**Reading date components**
```javascript
const d = new Date('2024-03-15T10:30:00Z');
// UTC methods — safe for date arithmetic
d.getUTCFullYear() // 2024
d.getUTCMonth() // 2 (March, 0-indexed)
d.getUTCDate() // 15 (day of month)
d.getUTCHours() // 10
d.getUTCMinutes() // 30
// Local methods — depend on the user's timezone
d.getFullYear() // may differ from UTC if offset crosses midnight
d.getMonth() // same caveat
```
**Timezone pitfalls**
```javascript
const d = new Date('2024-03-15'); // parsed as UTC midnight
d.toISOString(); // '2024-03-15T00:00:00.000Z' — UTC
d.getDate(); // may return 14 in UTC-5! (local time is previous day)
// Rule: use UTC methods for arithmetic, toISOString() for serialization
```
**Date arithmetic**
```javascript
const start = new Date('2024-01-01');
const end = new Date('2024-03-15');
const diffMs = end - start; // 6393600000 ms
const diffDays = diffMs / (1000 * 60 * 60 * 24); // 74 days
// Add 7 days to a date
const next = new Date(start);
next.setDate(next.getDate() + 7);
```
**Date.now() for performance timing**
```javascript
const t0 = Date.now();
doExpensiveWork();
console.log(`Took ${Date.now() - t0} ms`);
```
replacer, reviver, toJSON(), and structuredClone vs JSON roundtrip
**replacer — filter or transform during stringify**
The second argument to `JSON.stringify` can be an array of keys (whitelist) or a function called for each key/value:
```javascript
const user = { name: 'Alice', password: 'secret', age: 30 };
// Array replacer — keep only these keys
JSON.stringify(user, ['name', 'age']) // '{"name":"Alice","age":30}'
// Function replacer — transform values
JSON.stringify(user, (key, value) => {
if (key === 'password') return undefined; // omit sensitive fields
if (typeof value === 'number') return value * 2; // transform
return value;
});
// '{"name":"Alice","age":60}'
```
**reviver — transform during parse**
The second argument to `JSON.parse` is called for each parsed value, from the inside out. Use it to convert Date strings back to Date objects:
```javascript
const iso = /^\d{4}-\d{2}-\d{2}T[\d:.]+Z$/;
function dateReviver(key, value) {
if (typeof value === 'string' && iso.test(value)) {
return new Date(value);
}
return value;
}
const json = '{"name":"Event","date":"2024-03-15T00:00:00.000Z"}';
const obj = JSON.parse(json, dateReviver);
obj.date instanceof Date // true
obj.date.getUTCFullYear() // 2024
```
**toJSON() — customize serialization for a class**
If an object has a `toJSON` method, `JSON.stringify` calls it and uses the return value:
```javascript
class Temperature {
constructor(celsius) { this.celsius = celsius; }
toFahrenheit() { return this.celsius * 9/5 + 32; }
toJSON() { return { celsius: this.celsius, unit: 'C' }; }
}
const temp = new Temperature(100);
JSON.stringify(temp) // '{"celsius":100,"unit":"C"}'
// Without toJSON: '{"celsius":100}' (only own enumerable props)
```
**structuredClone vs JSON roundtrip**
```javascript
// JSON roundtrip — simple but loses non-JSON types
const clone1 = JSON.parse(JSON.stringify(obj));
// Dates become strings, undefined is dropped, Sets become {}
// structuredClone — preserves Date, Map, Set, ArrayBuffer, etc.
const clone2 = structuredClone(obj);
// Dates stay Date, Maps stay Map — but functions still can't be cloned
```
Use `structuredClone` for general deep-cloning; use JSON roundtrip only when you know the data is plain JSON-compatible.
Given a JSON array of event objects with 'date' fields (ISO strings), return the event with the most recent date.
function latestEvent(jsonStr) {
// your code here
}
const events = '[{"name":"Conf","date":"2024-09-12"},{"name":"Workshop","date":"2024-11-03"},{"name":"Meetup","date":"2024-07-20"}]';
console.log(latestEvent(events).name); // 'Workshop'
Solution
function latestEvent(jsonStr) {
const events = JSON.parse(jsonStr);
return events.reduce((latest, e) => e.date > latest.date ? e : latest);
}
Given an array of objects with a `toJSON()` method, demonstrate custom JSON serialization: each object should serialize as `{ type, value }` where type is the class name.
class Temperature {
constructor(celsius) { this.celsius = celsius; }
toJSON() {
// your code here — return the object to serialize
}
}
const temps = [new Temperature(100), new Temperature(0), new Temperature(37)];
const json = JSON.stringify(temps);
console.log(json);
// '[{"type":"Temperature","value":100},{"type":"Temperature","value":0},{"type":"Temperature","value":37}]'
Solution
class Temperature {
constructor(celsius) { this.celsius = celsius; }
toJSON() {
return { type: this.constructor.name, value: this.celsius };
}
}
No split tab
Cookie preferences
We use necessary cookies to run the site. With your permission, we can also save your site preferences and use analytics and advertising cookies to understand usage and support the project.
Open tools in tabs.Exercises, IDE tools, and trainers stay available as site tabs.
Switch without losing context.Move between explanations, code, and utilities while keeping your place.
Use the sidebar as your map.The left panels hold navigation, settings, files, libraries, and tool controls.
PythonJavaScriptSQLite
One IDE, three practical modes
Python in the browser.Run small scripts, try libraries, and practice API requests without installing anything.
JavaScript for quick experiments.Test browser-friendly code and compare ideas next to your learning materials.
SQLite for data practice.Open the database explorer to inspect tables, write queries, and learn SQL workflows locally.
TopicIDE
Work side by side with split tabs
Keep instructions visible.Open an exercise or reference page beside the IDE instead of jumping back and forth.
Compare tools while you learn.Place regex checks, explanations, and code experiments next to each other when the task needs it.
Close the split when you are done.The workspace returns to a single focused tab, and your open site tabs remain available.
Code Typing Trainer
Or plain text
This trainer is designed for a physical keyboard.Open this section on a laptop or desktop with a wide screen. Touch typing practice will not work correctly on a phone.
Speed: 0 chars/min
0 words/min
Best speed (60s): 0 chars/min
0 words/min
Errors: 0
Total time: 0.0 s
To practice touch typing, avoid looking at your physical keyboard.