Skip to content

Instantly share code, notes, and snippets.

@joannasese
Last active April 30, 2017 21:55
Show Gist options
  • Save joannasese/7cd5fcfc4bd649bf39600c32457169d6 to your computer and use it in GitHub Desktop.
Save joannasese/7cd5fcfc4bd649bf39600c32457169d6 to your computer and use it in GitHub Desktop.
Datatypes
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Datatypes">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Datatypes</title>
</head>
<body>
<script id="jsbin-javascript">
Stuff about datatypes.
</script>
<script id="jsbin-source-javascript" type="text/javascript">Stuff about datatypes.</script></body>
</html>
/*
DATATYPES:
0. Values in Javascript will be of a certain datatype. There are "primitive" or "simple" data types. There are also "complex" datatypes, which have the distinct honor of being called neither primitive nor simple.
1. Primitive Data Types
- Number
- NaN
- Infinity and -Infinity
- String
- Boolean
- undefined
- null
These values are referred to as primitive or simple because they aren't complicated! They are immutable, which means they do not change.
2. Complex Data Types
- Array
- Object
- Function
Complex data types can hold any number of values. To be more specific, arrays can hold any number of values, objects can hold any
number of key:value pairs, and functions can hold any number of statements. More on that later.
1a. NUMBERS: numeric data.
*/
var worstNumber = 53;
console.log(worstNumber); //prints -> 53
/*
NaN: Not a number.
NaN indicates that a value is not a number.
*/
Math.sqrt(-2); // Math.sqrt is used to find the squareroot of a number. Here we are trying to find the square root of a negative number.
console.log(Math.sqrt(-2)); // prints -> NaN
/*
Infinity and -Infinity
Infinity is a numeric value representing positive infinity, which is greater than any number. Negative infinity is a numeric value representing negative infinity,
1b. STRINGS: value by which we can express character data aka. words or phrases. Here's an example:
*/
var favoriteFlower = "Hellebore, the famed Lenten Rose!";
console.log(favoriteFlower); //prints -> "Hellebore, the famed Lenten Rose!"
/*
Notice that in order to express a string, you need to use quotations. Double and single quotations both work.
*/
var favoriteFlower = 'Hellebore, the famed Lenten Rose!';
console.log(favoriteFlower); //prints -> "Hellebore, the famed Lenten Rose!"
/*
Sidenote: Didn't we say that variables change? We did! But the values we assign to them do not change! They are unchangeable!
1c. BOOLEAN: true or false value
*/
var iAmHungry = true;
var timeToEat = false;
/*
1d. UNDEFINED: a variable that has not been assigned a value will have an undefined value
*/
var secretsToTheUniverse; // variable is not assigned a value
console.log(secretsToTheUniverse); // prints -> undefined
/*
1e. NULL: null expresses a lack of identification or no value
Null is easily confused with undefined. An undefined variable means a value has not been assigned yet. Null is a value in and of itself that represents no value.
Null needs to be assigned, while undefined usually happens because a value was not assigned.
*/
var oldDirt;
console.log(oldDirt); // prints -> undefined
var oldDirt = null;
console.log(oldDirt); // prints -> null
/*
2a. ARRAY: Arrays are variables that can store multiple values. They are easily identified by brackets.
*/
var scentedFlowers = ["gardenias", "roses", "jasmine"];
var lotteryNumbers = [3, 48, 98, 2];
/*
To access a value from an array, use its index number. For example, to pull "roses" from the variable scentedFlowers, identify the index number like so:
*/
console.log(scentedFlowers[1]); // prints -> "roses"
/*
2b. OBJECTS: Objects are variables that store key:value pairs. Keys are strings, while the values can be any value. They are easily identified by curly brackets.
*/
var plant = {
type: "tree",
name: "pine"
};
console.log(plant.type); // prints -> "tree"
/*
2c. FUNCTIONS: A function is a block of reusable code that is useful when you want to run different values through the same code. Think of them as recipes.
Functions prevent you from needing to write out the recipe every time an ingredient changes. Functions get their own chapter here, so for now, just remember what a function looks like:
*/
function (parameterOne, parameterTwo) {
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment