Literals

Literals are fixed values within JavaScript. Rather than assigning these values like variables, you insert values directly into the literal. There are seven types of literals within JavaScript:

1. Array Literals

An array is a group of values, indicated by square brackets [ ] using commas , to separate the values.

var players = ["Steph Curry", "Kevin Durant", "Klay Thompson"];

2. Boolean Literals

Only two values: true and false. These are different from the primitive types.

var happy = true;

3. Floating-point Literals

Essentially numbers with decimal points, but can also include exponents (similar to scientific notation) 3.2e12

4.1, 5.5, 10.1

4. Integers

Literally numbers. Can appear in multiple forms: Base 10 (0-9), Base 8 (octal; 0-7), Base 16 (hexidecimal; 0-e), or Base 2 (binary; 0-1)

5. Object Literals

Similar to the object data-type. Example:

let x = { a:0, b:1, c:2 }
console.log(x.a)// 0
console.log(x.b)// 1
console.log(x.c)// 2

6. RegExp Literals

RegExp will be explained further later. It is a pattern within slashes, like this: /ab+c/

7. String Literals

A set of characters within either single quotation marks ( ' ) or double quotation marks ( " ). Either can be used for a string, but they cannot be used together. Examples: 'hello', "hello"

File Location

    javascript-library
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
                06-literals.js <-- You are here

Practice

  1. In literals.js, create an array literal with 3 items and an object literal with 3 values.

  2. Convert the hexidecimal number ee to base 10 (decimal). Hint: 0e = 15

Last updated