console.log("Hello, World!");
node hello.js
// Declare dimensions of the rectangle using letlet length = 5; // length of the rectanglelet width = 3; // width of the rectangle
// Calculate area using constconst area = length * width;
// Print the area to the consoleconsole.log("The area of the rectangle is:", area);
node rectangleArea.js
function calculateSum(a, b) { return a + b;}
// Example of function scopinglet number1 = 8;let number2 = 12;const sum = calculateSum(number1, number2);
console.log("The sum of", number1, "and", number2, "is:", sum);
node sumFunction.js
// Define a class to represent a Bookclass Book { constructor(title, author, year) { this.title = title; this.author = author; this.year = year; }
// Method to display book details displayInfo() { console.log(`Title: ${this.title}, Author: ${this.author}, Year: ${this.year}`); }}
// Create an array to store the book objectslet bookstore = [];
// Function to add books to the bookstorefunction addBook(book) { bookstore.push(book);}
// Function to display all books in the bookstorefunction displayBooks() { bookstore.forEach(book => book.displayInfo());}
// Create book objectsconst book1 = new Book("To Kill a Mockingbird", "Harper Lee", 1960);const book2 = new Book("1984", "George Orwell", 1949);const book3 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
// Add books to the bookstoreaddBook(book1);addBook(book2);addBook(book3);
addBook(book1);addBook(book2);addBook(book3);
// Display all booksconsole.log("Displaying all books in the bookstore:");displayBooks();
node bookstore.js
// Define functions for basic arithmetic operations
function add(x, y) { return x + y;}
function subtract(x, y) { return x - y;}
function multiply(x, y) { return x * y;}
function divide(x, y) { if (y === 0) { console.log("Error: Division by zero is not allowed."); return undefined; } return x / y;}
// Export the functions so they can be used in other filesmodule.exports = { add, subtract, multiply, divide};
// Import the calculator moduleconst calculator = require('./calculator');
// Examples of using the imported functionsconst sum = calculator.add(5, 3);const difference = calculator.subtract(10, 4);const product = calculator.multiply(7, 6);const quotient = calculator.divide(20, 5);
// Display the resultsconsole.log(`Sum: ${sum}`);console.log(`Difference: ${difference}`);console.log(`Product: ${product}`);console.log(`Quotient: ${quotient}`);
node index.js
javascriptCopy code// Define some variables to use within template literalsconst user = { name: "James T. Kirk", role: "Captain", ship: "USS Enterprise"};const missionCount = 112;
// Create a complex string using template literalsconst introduction = `Hello, my name is ${user.name}, and I am the ${user.role} of the ${user.ship}.I have commanded over ${missionCount} missions across various galaxies.Today's date is ${new Date().toLocaleDateString()}.`;
// Use template literals to perform calculations directly within the stringconst operationalYears = 5;const report = `As of ${new Date().getFullYear()}, the ${user.ship} has been operational for ${operationalYears} years.In this time, we've completed ${Math.floor(missionCount / operationalYears)} missions per year on average.`;
// Display the resultsconsole.log(introduction);console.log(report);
bashCopy codenode templateLiterals.js
