In programming, a data type is a classification identifying one of various types of data.
Using data types, we can determine
"Kittens Are Awesome!!!"
"Cute"
"45"
"dfasdfsadjflsakdfj"
42
109
23423423
42.42
109.23423424
23423423.23423424234234
All numbers in JavaScript are "double-precision 64-bit format IEEE 754 values" ==> there's really no such thing as an integer in JavaScript.
Be a careful with your arithmetic if you're used to math in other programming languages.
0.1 + 0.2
=> 0.30000000000000004
Represents either true or false |
true
false
Ordered Collections of Data |
goldenGirls = ["Dorothy", "Blanch", "Sofia", "Rose"]
Key/value paired Collections of Data |
bestHero = {
name: 'Aquaman',
power: ['swimming','being fabulous', 'lots of sea friends'],
hairGame: true,
}
2 + 2 // returns 4
4 - 2 // returns 2
4 / 2 // returns 2
4 * 2 // returns 8
// What does the following
// statement return?
"hello" + "world"
typeof(37) === 'number';
=> true
typeof({}) === 'object';
=> true
typeof('hi there') === 'string';
=> true
// 3^2 becomes
Math.pow(3,2)
=> 9
// 2^4 becomes
Math.pow(2,4)
=> 16
number to nth power
// √(4) becomes
Math.sqrt(4)
=> 2
square root
// The following only returns a random decimal
Math.random()
=> 0.229375290430
/**
The following will return a
random number between 0 and 10
*/
Math.random()*10
get a random number
// Remove the decimal
Math.floor(3.14)
=> 3
Math.floor(3.9999)
=> 3
turn a float to a number
var a = 2;
var b = "kittens are cute";
var myArray = ['one', 'two'];
a ;
//returns 2
b;
// returns "kittens are cute"
myArray
// returns ['one', 'two']
var a = 5;
a = 6;
a;
// returns 6
var a = 5;
a +=;
a;
// returns 6
++, --, -= can also
be used!
Arrays are great for:
Storing data
Enumerating data (i.e., using an index to find them)
Quickly reordering data In essence, arrays compose a data structure that is similar in concept to a list. Each item in an array is called an element, and the collection can contain data of the same or different types. In JavaScript, arrays can dynamically grow and shrink in size.
var friends = ['Moe', 'Larry', 'Curly'];
=> ['Moe', 'Larry', 'Curly']
// First friend
var firstFriend = friends[0];
=> 'Moe'
// Get the last friend
var lastFriend = friends[2]
=> 'Curly
var a = ["dog", "cat", "hen"];
a.length;
=> 3
get the length of your array
var a = ['one', 'two', 'three'];
a[0];
// return 'one'
a[1];
// return 'two'
a[2];
// return 'three'
a.length;
// return 3
var a = new Array();
=> undefined
a[0] = "dog";
=> "dog"
a[1] = "cat";
=> "cat"
a[2] = "hen";
=> "hen"
a
=> ["dog", "cat", "hen"]
a.length;
=> 3
var a = ["dog", "cat", "hen"];
a;
//returns ["dog", "cat", "hen"]
a[2];
// "hen"
var goods = new Array("water", "coffee")
var myArray = ['frogs','dogs','cats'];
myArray;
// returns ['frogs','dogs','cats']
var goods = new Array("water", "coffee")
goods;
//returns ['water', 'coffee']
var a = [], // these are the same
b = new Array(), // a and b are arrays with length 0
c = ['foo', 'bar'], // these are the same
d = new Array('foo', 'bar'), // c and d are arrays with 2 strings
// these are different:
e = [3] // e.length == 1, e[0] == 3
f = new Array(3), // f.length == 3, f[0] == undefined
Use bracket notation to add values to the one of the arrays;
goods[2] = "beer"
use the .push() method for the other.
quantity.push(8)
=> 'Today, I consumed ' + quantity[0] + ' cups of ' + goods[0]
=> 'I am going to have ' + quantity[1] + ' cups of ' + goods[1] + ' tonight'
var message = []
message.push(8)
=> 1
message.push('r', 'e', 'b', 'm', 'u')
=> 6
message.push('n', 's', 'i', 'A', 'G', 'K')
=> 12
message
=> [ 8, 'r', 'e', 'b', 'm', 'u', 'n', 's', 'i', 'A', 'G', 'K' ]
message.pop()
=> 'K'
message.shift()
=> 8
message.unshift(1)
=> 11
message.reverse()
=> [ 'G', 'A', 'i', 's', 'n', 'u', 'm', 'b', 'e', 'r', 1 ]
message.join()
=> 'G,A,i,s,n,u,m,b,e,r,1'
message.join(' ')
=> 'G A i s n u m b e r 1'
var goldenGirls = ['Blanch', 'Dorothy', 'Rose', 'Sophia'];
for (var i = 0; i < goldenGirls.length; i++) {
console.log(goldenGirls[i]);
}
var goldenGirls = ['Blanch', 'Dorothy', 'Rose', 'Sophia'];
goldenGirls.forEach(function(el) {
console.log(el);
});
var evens = []
evens.push(2,4,6,8,10)
=> 5
var odds = []
odds.push(1,3,5,7,9)
=> 5
evens.every(function (num) {
return num % 2 === 0
})
=> true
evens.every(function (num) {
return num % 4 === 0
})
=> false
var odds = []
odds.push(1,3,5,7,9)
=> 5
var array = [1, 2, 3, 4, 5];
var even = function(element) {
// checks whether an element is even
return element % 2 === 0;
};
console.log(array.some(even));
// expected output: true
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
var timesFive = evens.map(function (num) {
return num * 5
})
=> undefined
timesFive
=> [10, 20, 30, 40, 50]
var timesTen = odds.map(function (num) {
return num * 10
})
=> undefined
timesTen
=> [10, 30, 50, 70, 90]
For this assignment, you'll create your own Startup Generator app. Open the files in the madlib with loops folder and start by reading the "instructions.md" file. You'll learn how to use helper methods and for loops. We have provided some starter code; while we haven't covered functions and DOM manipulation yet, this is a good chance for you to challenge yourself and get a head start on these topics.