JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Examples of each
  • boolean
  • null
  • undefined
  • number
  • string
  • symbol
  • Object
  • Dynamically-typed
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 1-Grammar and Types

Types

JavaScript has seven different defined data-types:

  1. boolean: Binary data-type. Only valid values are true or false.

  2. null: keyword that denotes a null value. This is not 0. It is the absence of value

  3. undefined: a data-type that has not been defined with a value.

  4. number: a numerical data-type.

  5. string: any combination of characters to be read as text.

  6. symbol: used to make anonymous object properties.

  7. object: a container which can hold multiple data-type values.

Examples of each

boolean

let x = true;
console.log(x); //true

null

let x = null;
console.log(x); //null = no value;

undefined

let x;
console.log(x); //undefined - no value assigned

number

let x = 17;
console.log(x); //17

string

let x = 'Hello World!';
console.log(x); //Hello World!

symbol

const MY_KEY = Symbol();
const obj = {
    [MY_KEY]: 123
};

Object

let x = {
    hello: 'test',
    number: 13
};
console.log(x.hello); //'test'
console.log(x.number); //13

Dynamically-typed

JavaScript is a dynamically-typed language, meaning that variables aren't assigned a specific type. You can see this in action with the following example:

let x = 19;
console.log(x); //x
x = 'tree';
console.log(x); //'tree'
x = false;
console.log(x); //false

A single variable can be assigned multiple types, but only one type at any given time. Each time a value is assigned, it replaces the previous value.

File Location

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

Practice

  1. In types.js, create a variable for each type and print each to the console.

  2. Create a new variable. Use the typeof keyword to print to the console:console.log(typeof x)

  3. Change the value of that variable to a different data-type, then use typeof again.

  4. What happens? How can this be utilized in a program?

PreviousHoistingNextLiterals

Last updated 7 years ago