Skip to content

Instantly share code, notes, and snippets.

@nicolegooden
Last active May 28, 2020 01:47
Show Gist options
  • Save nicolegooden/2c72d5630337db7709e8dc1a72c9fb80 to your computer and use it in GitHub Desktop.
Save nicolegooden/2c72d5630337db7709e8dc1a72c9fb80 to your computer and use it in GitHub Desktop.
Mod0_S1_HW_Practice_Tasks

JavaScript Data Types

Javascript_image

Data types represent the type of data that is used in a programming language.

Strings

A string is a data type used to represent text.

Strings are inclusive of numbers, letters, spaces, and special characters.

Strings represent a piece of information that is constant.

Here are two examples of a string data type:

  1. <p>"Turing is providing an amazing opportunity for prospective software engineers"</p>

  2. "(123) 456 - 7890"

Integers

An integer is a whole number ranging from negative numbers to positive numbers, inclusive of 0.
Integers are not decimal numbers, nor are they fractions.

Here are two examples of an integer data type:

  1. 38

  2. -600

Floats

A float is a decimal number that can be either negative or positive.

Here are two examples of a float data type:

  1. -8.65

  2. 3.62

Booleans

A boolean is a data type that has a value of either true or false, typically in response to a true/false scenario such as an if/else conditional statement.

When determining whether a statement is true or false, operators can be used within the statement that is being assessed.

  • && is an operator for 'and'
  • || is an operator for 'or'
  • == is an operator for 'equal in value but not necessarily in data type'.

When it comes to the == operator, something like 3 == "three" would return true even though 3 is an integer and "three" is a string.

Using the === operator means the values on either side of the operator must match according to data type and value.

In this case, 3 === "three" would return false, while 3 === 3 and "three" === "three" would both return true.

  • If a statement includes the && operator, statements on both sides of the operator must be true in order to return a boolean value of true.

  • If a statement includes the || operator, at least one statement on either side of the operator must be true in order to return a boolean value of true.

Here are the only two values of a boolean:

  1. true

  2. false

Arrays

An array is a data structure that represents a collection of similar items.

It is best practice to include a single data type as items in an array, rather than mixing two data types such as an integer and a string.

The syntax of an array includes [ ], where all items in the collection are written inside the brackets.

Here are two examples of an array data type:

  1. Including integers only:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1. Including strings only:
["Santa", "Matt", "Nicole", "Rio", "Tom", "Harper"]

Extensions

A hash or object is a collection of key-value pairs.

For this data type, items are paired together rather than listed as independent items in a collection, the way they are laid out in an array.

With a hash or object, two pieces of information are meant to have a purposeful relationship.
The syntax of a hash or object includes { }, or curly braces.

Here are two examples of an extension:

  1. {"Ms. Gooden's Class": 26, "Ms. Trecek's Class": 27, "Mr. Bartko's Class": 25}

where the key represents the teacher's name and the value represents the number of students in each teacher's class.

  1. {"Fifth Grade": "Ms. Trecek, Ms. Gooden, and Mr. Bartko", "Fourth Grade": "Ms. Franco, Ms. Farnell, and Ms. Barber"}

where the key represents the elementary grade level and the value represents the names of the teachers per each grade level.}

Conclusion

To conclude, there are 6 data types defined and explained in this gist. Each data type has a purpose and exists in Javascript code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment