Learning JavaScript: Data Types and Variables

Learning JavaScript: Data Types and Variables

To paraphrase the title of an old computer science textbook, “Algorithms + Data = Programs.” The first step in learning a programming language such as JavaScript is to learn what types of data the language can work with. The second step is to learn how to store that data in variables. In this article I’ll discuss the different types of data you can work with in a JavaScript program and how to create and use variables to store and manipulate that data.

Image for post

To paraphrase the title of an old computer science textbook, “Algorithms + Data = Programs.” The first step in learning a programming language such as JavaScript is to learn what types of data the language can work with. The second step is to learn how to store that data in variables. In this article I’ll discuss the different types of data you can work with in a JavaScript program and how to create and use variables to store and manipulate that data.

JavaScript Data Types

Unlike a compiled programming language, JavaScript does not require you to declare the data type of a variable. However, internally, JavaScript keeps track of the type of data being stored in a variable.

The three fundamental types of data (also known as primitives) you can use in a JavaScript program are:

number: numeric values such as 100 or 3.14159

string: Textual data, which can include numbers and symbols

boolean: true or false values

There are other primitive types (BigIntSymbol, and undefined) but they are not important for your beginning education in JavaScript. There are three other data types in JavaScript — objectnull, and function — but I will also save the discussion of these types for another time.

Literal Data

When data is encountered in a program, that data is said to be literal data. For example, if I write:

print(100);

The value 100 is called a number literal. If I write:

print("Hello, world!");

The phrase “Hello, world!” is a string literal. Literals are found throughout a program and I needed to describe what literal data is to distinguish it from a variable, which is just a name for a storage location in memory that can hold data.

Determining the Data Type of a Literal

You can see what the type of a literal is by calling a special function: typeof. This function returns the type of data passed into it. Here are some examples of calling the typeof function on some literal data:

typeof(100); // returns "number"
typeof("hello"); // returns "string"
typeof(false); // returns "boolean"

As you become more experienced in JavaScript programming, you will find the typeof function useful for avoiding all types of logic errors in your programs.

learning-javascript learn-to-program javascript javascript-tips programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Learning JavaScript: Development Environments for JavaScript Programming

One of the nice things about learning JavaScript these days is that there is a plethora of choices for writing and running JavaScript code. In this article, I’m going to describe a few of these environments and show you the environment I’ll be using in this series of articles.

Learning JavaScript: for Loops

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type. I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.

Learning JavaScript: Computing with Object Methods

JavaScript has a set of built-in methods you can use with your user-defined objects. In this article I’m going to discuss several of these methods and how you can use them in your JavaScript programs.

Learning JavaScript: Working with Strings

Strings are the second most common data type used in JavaScript, and in many cases, since JavaScript is so widely used for web applications, it is the prominent data type.

Learning JavaScript: Statements, Arithmetic, and Math

In this article I will discuss how to perform arithmetic and more advanced mathematical operations in JavaScript. First, though, I need to discuss how statements are formed and used in JavaScript.