DEV Community

Cover image for JavaScript from absolute zero · 2. Values, variables and types
Amit chakraborty
Amit chakraborty

Posted on Originally published at amitchakraborty.dev

JavaScript from absolute zero · 2. Values, variables and types

JavaScript from absolute zero · Chapter 2 of 14 · Languages · new chapter every Tuesday morning

By the end of this chapter: Hold the entire value model of the language in your head.

The problem

Emma is a marketing assistant who has just been asked to test a tiny piece of JavaScript that a colleague promised would “show the current date on any computer”. She opens the shared folder, double‑clicks the file show-date.js, and a blank window flashes before disappearing. The file contains the single line

console.log(new Date());
Enter fullscreen mode Exit fullscreen mode

Emma is confused because she expected to see the current date displayed on her screen, but instead, the window disappeared immediately. She doesn't understand what console.log means or why the window closed so quickly. She also wonders what new Date() does and why it's not showing her the date.

Emma's problem is that she doesn't understand the basics of JavaScript values, variables, and types. She needs to learn what these concepts mean and how they work together to produce the desired output. In this chapter, we will explore these fundamental concepts and help Emma understand how to write and run JavaScript code effectively.

Before you start

To follow along with this chapter, you need to have Node.js installed on your machine, and you should have completed the previous chapter, "What a program is, and running your first line today". You should also have a basic understanding of how to use a terminal or command prompt. If you're not sure how to use a terminal, don't worry – we'll cover the basics as we go along.

To verify that you have Node.js installed, open a terminal and type the following command:

node -v
Enter fullscreen mode Exit fullscreen mode

This should display the version of Node.js installed on your machine. If you don't see a version number, you'll need to install Node.js before proceeding.

Understanding values

In JavaScript, a value is a piece of information that can be stored and used in a program. Values can be numbers, words, dates, or even more complex things like objects and arrays. In Emma's code, new Date() is an example of a value – it's a way of getting the current date and time.

Let's try creating some simple values in JavaScript. Open a terminal and type the following command:

node
Enter fullscreen mode Exit fullscreen mode

This will open the Node.js REPL (Read-Evaluate-Print Loop), where you can type JavaScript code and see the results immediately. Try typing the following:

console.log(5);
console.log("hello");
console.log(new Date());
Enter fullscreen mode Exit fullscreen mode

You should see the number 5, the word "hello", and the current date and time printed to the console.

Variables and assignment

In JavaScript, a variable is a name given to a value. Variables allow you to store and reuse values in your program. To create a variable, you use the let keyword followed by the name of the variable and the value you want to assign to it.

Try typing the following in the Node.js REPL:

let name = "Emma";
console.log(name);
Enter fullscreen mode Exit fullscreen mode

You should see the word "Emma" printed to the console. You can also assign a new value to an existing variable using the assignment operator (=).

name = "John";
console.log(name);
Enter fullscreen mode Exit fullscreen mode

You should see the word "John" printed to the console.

Types

In JavaScript, every value has a type. The type of a value determines what kind of operations you can perform on it. For example, you can add numbers together, but you can't add a number and a word.

Try typing the following in the Node.js REPL:

console.log(5 + 3);
console.log("hello" + " world");
Enter fullscreen mode Exit fullscreen mode

You should see the result of the addition (8) and the concatenated string ("hello world") printed to the console.

Build it

Now that we've covered the basics of values, variables, and types, let's build a simple program that demonstrates these concepts. Create a new file called greeting.js and add the following code:

let name = "Emma";
console.log("Hello, " + name + "!");
Enter fullscreen mode Exit fullscreen mode

Save the file and run it using Node.js:

node greeting.js
Enter fullscreen mode Exit fullscreen mode

You should see the message "Hello, Emma!" printed to the console.

When this breaks

Here are a few common errors you might encounter when working with values, variables, and types:

  • Undefined variable: If you try to use a variable that hasn't been declared, you'll get a ReferenceError. For example:
console.log(name);
Enter fullscreen mode Exit fullscreen mode

If name hasn't been declared, you'll see an error message like "ReferenceError: name is not defined".

  • Type mismatch: If you try to perform an operation on a value that's not the right type, you'll get a TypeError. For example:
console.log("hello" + 5);
Enter fullscreen mode Exit fullscreen mode

This will work, but if you try to add a number to a non-numeric value, you'll get a NaN (Not a Number) result.

  • Assignment error: If you try to assign a value to a variable that's not a valid assignment, you'll get a SyntaxError. For example:
let name =;
Enter fullscreen mode Exit fullscreen mode

This will give you a SyntaxError because you can't assign a value to a variable without specifying the value.

What it costs

Using variables and types in JavaScript has some trade-offs:

  • Memory usage: Variables take up memory, so if you have a lot of variables, your program might use more memory than it needs to.
  • Performance: Some operations, like type checking, can slow down your program.
  • Readability: Using variables and types can make your code more readable, but it can also make it more complex if you're not careful.

In the interview

In an interview, you might be asked questions like:

  • "What's the difference between a value and a variable in JavaScript?"
  • "How do you declare a variable in JavaScript?"
  • "What's the type of the value 5 in JavaScript?"

A weak answer might be: "Um, I think a value is like a thing, and a variable is like a name for the thing...?"

A stronger answer would be: "In JavaScript, a value is a piece of information that can be stored and used in a program, like a number or a string. A variable is a name given to a value, so you can store and reuse it. To declare a variable, you use the let keyword followed by the name of the variable and the value you want to assign to it. The type of the value 5 is a number, which is a primitive type in JavaScript."

Your tasks

  1. Create a new file called calculator.js and add a variable x with the value 5. Then, add a variable y with the value 3. Finally, log the result of x + y to the console.
  2. Create a new file called greeting.js and add a variable name with the value "John". Then, log a greeting message to the console using the name variable.
  3. Create a new file called type-check.js and add a variable x with the value 5. Then, try to add a string to x and log the result to the console. What happens?

Remember to run each file using Node.js and verify the output. If you get stuck, don't hesitate to ask for help!


Your tasks this week

Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.

Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.

JavaScript from absolute zero

Chapter 2 of 14. New chapter every Tuesday morning.
Next: Making decisions: conditions and the traps.

· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe


Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.

Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.

Top comments (0)