Blog>
Snippets

Importing a single function from a module

Showcase how to import a specific function from a module, use it in a function and log the output.
// Assuming we have a module named 'mathUtils' with a function named 'square'
import { square } from './mathUtils';

// Using the imported function in another function to compute the square of a number
function calculateSquareAndLog(number) {
    const squaredNumber = square(number);
    console.log(`The square of ${number} is ${squaredNumber}.`);
}

calculateSquareAndLog(5); // Should log: 'The square of 5 is 25.'
This code snippet demonstrates how to import a single function named 'square' from a module located at './mathUtils'. It then uses this function in another function called 'calculateSquareAndLog', which takes a number as an argument, uses the 'square' function to calculate its square, and logs the result to the console.