Warm up: Assigning variables

A+ presents the exercise submission form here.

Data-types

Data types basically specify what kind of data can be stored and manipulated within a program. In the previous warm up exercise, you were tasked with assigning a value to the declared variable. Depending on your approach, you could’ve either used a Number or a String as the variables data type. Since JavaScript is a dynamically typed (loosely typed) language, it automatically determines the variables’ data type for you based on the values you use.

There are six basic data types in JavaScript which can be divided into three main categories: primitive (or primary), composite (or reference), and special data types. String, Number, and Boolean are primitive data types. Object, Array, *and *Function (which are all types of objects) are composite data types, whereas Undefined and Null are special data types.

Data types that are primitive can hold only one value at a time, whereas composite data types can hold combinations of values and more complex entities. Let’s examine each one in detail.

String

The string data type is used to represent textual data (i.e. sequences of characters). Strings are created using single or double quotes surrounding one or more characters, as shown below:

const string1 = 'Hello there!' //Using single quotes
const string2 = "General Kenobi!" //Using double quotes

You can also include quotes inside the string as long as they don’t match the enclosing quotes:

const string1 = "Let's have a cup of coffee."; // single quote inside double quotes
const string2 = 'He said "Hello" and left.';  // double quotes inside single quotes
const string3 = 'We\'ll never give up.';     // escaping single quote with backslash
const string4 = `We'll never give up.`; //Backticks + single quotes.

Number

Number represents integer and floating numbers (decimals and exponentials). For example,

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

A number type can also be +Infinity, -Infinity, and NaN (not a number). For example,

const number1 = 1/0; //+Infinity
const number2 = -1/0; //-Infinity
// strings can't be divided by numbers
const number3 = "abc"/3; //NaN

Boolean

Boolean data type represents logical entities. Boolean represents one of two values: true or false. Notice the lack of quotes and lowercase letters in the values.

const bool1 = true;
const bool2 = false

Object

An object is a complex data type that allows us to store collections of data.

const student = {
    number: 007,
    name: "James Bond",
    school: "cambridge"
}

Array

An array is a type of object used for storing multiple values in single variable. Each value (also called an element) in an array has a numeric position, known as its index, and it may contain data of any data type-numbers, strings, booleans, functions, objects, and even other arrays. The array index starts from 0, so the first array element is arr[0].

const names= ["james", "john", "jack"];
console.log(names[0]); // "james"

undefined

The undefined data type represents value that is not assigned. If a variable is declared but the value is not assigned, then the value of that variable will be undefined:

let name;
console.log(name); // undefined
//Explicitly assigning undefined is also possible
name = undefined;
console.log(name); // undefined

null

Another special data type that can have only one value-the null value. A null value means that there is no value. It is not equivalent to an empty string (“”) or 0, it is simply nothing.

let name = null;
console.log(name); // null
//Explicitly assigning undefined is also possible

String handling

Note: the whole sentence, also the latter part “the student shouted” belongs to the message

A+ presents the exercise submission form here.

Arrays

A+ presents the exercise submission form here.

Object

A+ presents the exercise submission form here.

Note array

A+ presents the exercise submission form here.