DeveloperBreeze

Calculate Distance Between Two Points

// Function to calculate the distance between two points in a 2D plane
function calculateDistance(x1, y1, x2, y2) {
    const deltaX = x2 - x1;
    const deltaY = y2 - y1;
    return Math.sqrt(deltaX ** 2 + deltaY ** 2);
}

// Calculate and log the distance between two points (e.g., (0, 0) and (3, 4))
const distance = calculateDistance(0, 0, 3, 4);
console.log('Distance Between Two Points:', distance);

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
javascript

Running JavaScript in the Browser Console

  • Arrow keys: Navigate through previous commands.
  • Shift+Enter: Write multi-line code.
  • The console displays detailed error messages to help debug code.

Dec 10, 2024
Read More
Tutorial
javascript

Mastering console.log Advanced Usages and Techniques

You can measure the time it takes for a block of code to execute using console.time and console.timeEnd. This is useful for performance testing.

console.time('loop');
for (let i = 0; i < 1000000; i++) {
  // Some operation
}
console.timeEnd('loop'); // Output: loop: <time in ms>

Sep 02, 2024
Read More
Code
javascript

Parse JSON String to Object

// Example JSON string
const jsonString = '{"name":"John","age":30,"city":"New York"}';

// Parse the JSON string into a JavaScript object
const parsedObject = JSON.parse(jsonString);

// Log the parsed object to the console
console.log('Parsed Object:', parsedObject);

Jan 26, 2024
Read More
Code
javascript

Validate Password Strength

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Convert Array of Objects to CSV

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Calculate Greatest Common Divisor (GCD) of Two Numbers

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Detect Dark Mode Preference

// Check if the user prefers dark mode using window.matchMedia
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;

// Log whether dark mode is preferred by the user
console.log('Dark Mode:', isDarkMode);

Jan 26, 2024
Read More
Code
javascript

Detecting Browser and Version

// Immediately Invoked Function Expression (IIFE) to detect the browser and version
const browser = (function() {
    // Get user agent string from navigator
    const userAgent = navigator.userAgent;

    // Regular expression to match browser name and version
    const match = userAgent.match(/(chrome|firefox|safari|opera|edge|msie)\/(\d+(\.\d+)?)/i);

    // Return the detected browser information or 'Unknown' if not matched
    return match ? { name: match[1], version: match[2] } : 'Unknown';
})();

// Logging the detected browser information
console.log('Browser:', browser);

Jan 26, 2024
Read More
Code
javascript python +1

Generate Random Password

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!