Setting Up Node.js and Writing a Simple JavaScript Program.
Course Title: Modern JavaScript Programming: From Fundamentals to Full-Stack Development Section Title: Introduction to JavaScript and Setup Topic: Install Node.js and write a simple JavaScript program using modern ES6 syntax.(Lab topic)
Objective:
By the end of this lab, you will be able to install Node.js, set up a development environment, and write a simple JavaScript program using modern ES6 syntax.
Prerequisites:
Before starting this lab, make sure you have completed the following topics:
- JavaScript overview: History, role in web development, and runtime environments (browser, Node.js)
- Setting up a development environment with Visual Studio Code, Node.js, and npm
- Basic syntax: Variables (var, let, const), data types, operators, and expressions
- Running JavaScript in the browser console and via Node.js
Installing Node.js:
To install Node.js, follow these steps:
- Go to the Node.js download page and select the correct installer for your operating system.
- Download and run the installer, following the prompts to complete the installation.
- Once installed, open a terminal or command prompt and type
node -v
to verify that Node.js has been installed successfully.
Setting up a Development Environment:
In this lab, we will use Visual Studio Code (VS Code) as our development environment. To set up VS Code, follow these steps:
- Download and install VS Code from the official website.
- Create a new folder for your project and navigate to it in the terminal or command prompt.
- Open VS Code and select the folder you created as the workspace.
Writing a Simple JavaScript Program:
Create a new file called hello.js
in your project folder and add the following code:
// Import the console module
import console from 'console';
// Define a function that prints a greeting
function greet(name) {
console.log(`Hello, ${name}!`);
}
// Call the function with your name
greet('John');
This code imports the console module, defines a function that prints a greeting, and calls the function with a name.
Running the Program:
To run the program, follow these steps:
- Open a terminal or command prompt in your project folder.
- Type
node hello.js
to run the program. - Observe the output in the console.
Tips and Variations:
- Try changing the name in the
greet
function call to see the output change. - Experiment with different console methods, such as
console.log()
,console.info()
, andconsole.error()
. - Write a new function that takes a name and age as arguments and prints a greeting with the age.
Key Concepts:
- Node.js is a JavaScript runtime environment that allows you to run JavaScript on the server-side.
- ES6 syntax is a modern JavaScript syntax that provides features such as imports, exports, and arrow functions.
- The
console
module is a built-in module in Node.js that provides functions for printing output to the console.
Practical Takeaways:
- You can install Node.js using the official installer.
- You can set up a development environment with VS Code and Node.js.
- You can write a simple JavaScript program using modern ES6 syntax.
Next Steps:
In the next topic, we will cover conditionals (if, else, switch) and looping structures (for, while, forEach). You can review the topic and try out the examples to reinforce your understanding.
Comments and Help:
If you have any questions or need help with the lab, please leave a comment below. If you are stuck or need further clarification, please ask for help.
Images

Comments