Categories
JavaScript Tutorials

Value types

What Are Value Types?

In JavaScript, data is represented with values. There are four value types to convey data with: string, number, boolean, and object.

The simplest way to create a string, number, boolean, or object value in JavaScript is to literally type it into your script.

Literals

Creating a String Literal

Just wrap some text in a pair of double or single quotation marks, and you have yourself a string.

Example: “Ben & Jerry’s” or ‘Chocolate Fudge Brownie’

Gluing Strings Together with the + Operator

To glue two strings together, separate them with the + concatenation operator.

Example:

 “Ben & Jerry’s” + ” ” + “Chocolate Fudge Brownie” + ” is my favorite icecream.”;

Output: 

“Ben & Jerry’s Chocolate Fudge Brownie is my favorite icecream.”

Creating a Number Literal

Scripts typically do a lot of math. So, JavaScript, of course, has a number value type.

Example:

Chocolate Fudge Brownie has 4 servings per pint and 260 calories per serving. So, we could have JavaScript calculate the calories per pint with the * operator, which multiplies its operands:

4 * 260;

Output:

1040

Creating a Boolean Literal

Sometimes you will want a simple yes or no answer from JavaScript. In those circumstances, the return value for an expression will be true for yes and false for no.

Example:

“Chocolate Fudge Brownie” === “chocolate icecream”;

Output:

false