Data Types

JavaScript data types

JavaScript has 6 data types:

  • Numbers
  • String (text)
  • Boolean
  • Undefined
  • Null
  • Objects

Numbers

Numbers are any number without quotes, including numbers with decimals

For example: -10, 258, 3.14.

Strings

Strings are characters wrapped in single or double quotes.

Example: ‘Sample String’

Strings Methods

JavaScript provides a number of string methods.

We can use these methods with:

  • a period (the dot operator)
  • the name of the method
  • opening and closing parentheses
'example string'.methodName()

Example

let str = "hello";
let a = str.toLowerCase
console.log(a);
// hello

String Concatenation The + operator is used to concatenate strings.

console.log('Hello'+ ' ' + 'World');
// Hello World

However, we should use tempate strings when building up strings. For example:

function sayHi(name) {
  return `How are you, ${name}?`;
}

Undefined

undefined represents lack of defined value.

Variables that are declared but not initialized to a value will have the value undefined.

> var a
undefined

Null

null represents the intentional absence of a value

Boolean

Boolean has 2 values: true or false.

In Javascript, 0, empty strings, undefined, null, and NaN (Not-A-Number) are all considered falsey values.

Objects

Object is the collection of related data.

let dog = {
    name: "Puppy",
    age: 2,
    color: "gray"
}

name, age, and color are the properties of the dog object. We access the values of these properties using the dot or bracket notation.

// Using dot
console.log(dog.name) // Puppy

// Using bracket
console.log(dog["age"]) // 2