Learning Goals
- Run JavaScript with Node.js.
- Use
console.logto print output. - Create your first variable with
let. - Read code line by line and explain what each line means.
JavaScript Foundations
Welcome. We are starting from scratch with Node.js, variables, and console.log.
console.log to print output.let.When we write this, we mean we are saving text inside a variable and printing it.
// Save as lesson1.js
let message = "Hello, Patricia";
console.log(message);
Line by line:
let message = "Hello, Patricia"; means: create a variable named message and store text inside it.console.log(message); means: print whatever is inside message to the terminal.node lesson1.js in your terminal.Think of a variable like a labeled container.
message"Hello, Patricia"console.log opens the container and shows the value.So when we write code, we are really saying: "Store this value, then show it."
In this first lesson, your main skill is not typing fast. Your main skill is understanding what each line tells the computer to do. JavaScript reads your instructions from top to bottom. So if you create a variable first and print second, the output makes sense. If you try to print before creating the variable, the program fails because that value does not exist yet.
That is why we are keeping this lesson very simple: one variable, one print, clear meaning. Once this pattern is natural to you, every bigger topic becomes easier because most programs are still built from the same two actions: store values and use values.
Hello should be "Hello".console.log(message) before creating message.name and store your name.city and store your city.console.log.favoriteFood and print: I like ....Create a file named about-me.js with 4 variables: name, city, favoriteFood, and bestSubject. Print all of them one by one.
let name = "Patricia";
let city = "Kampala";
let favoriteFood = "Rolex";
let bestSubject = "JavaScript";
console.log(name);
console.log(city);
console.log(favoriteFood);
console.log(bestSubject);