Skip to content

Instantly share code, notes, and snippets.

@gonewayword
Forked from anonymous/index.html
Last active August 29, 2016 00:39
Show Gist options
  • Save gonewayword/09ed782c66090c1f6eaf550b300e4f58 to your computer and use it in GitHub Desktop.
Save gonewayword/09ed782c66090c1f6eaf550b300e4f58 to your computer and use it in GitHub Desktop.
Datatypes: Simple and complexDescribing datatypes in excessive detail// source http://jsbin.com/vofezuh
//II. Datatypes (Simple & Complex)
//Javascript has many data types that can be used in variables. These are, as they sound,
//types of data that are recognized by the interpreter as data. Below are some of the data
//types described.
//1. Number
//A number in javascript is, well, a number. It is declared using straight up integers or
//integers with decimal places, declared with a "." then the remaining part of the number,
//up to 15 decimal places. Each number is stored in 64 bits of memory.
//2. String
//A string is, technically, anything stored within two quotes--whether double quotes or
//single quotes. A string can be represented with any characters inside of those quotes,
//although certain characters can cause interruptions with the string, such as including a
//quote (of the same type that opened the string) inside of the string. This will cut the
//string off early.
//correct//
"use strict";
var me = "Brendan";
console.log(me);
//incorrect
//var myFavoriteQuote = "My favorite quote is "A witty saying proves nothing"";
//correct
var myFavoriteQuote = 'My favorite quote is "A witty saying proves nothing"';
//or also correct
var myFavoriteQuote = "My Favorite quote is \"A witty saying proves nothing\"";
console.log(myFavoriteQuote);
//3. Boolean
//Booleans are the heart of computers, from what I understand, and go back to
//how computing began--ones and zeroes. A boolean in javascript takes the values
//true or false. These values can be used in many useful ways, especially in
//program aspects where you have a yes/no, an on/off, or any other binary
//type of function. You can test if something is true or false using
//Boolean(10 = 10)
//Booleans are able to tell you what is truthy or falsey, as well. Zero, null,
//and undefined are "falsey" in that they represent something that does not exist.
//Booleans are also useful in many, many ways in javascript, for functions and objects.
//whether they are being used explicitly as true or false or operating in the background, they
//are all around. For just one example of booleans working covertly, think of an if else.
//A boolean can be crucial in an if else statement, for an example, to determine if the
//program should move forward, or the control flow should stop there. Essentially by
//saying if X is true, then do this, if X is false, then do this. For example:
function sleepTimer(time) {
if (time >= 9) {
return "Wake UP!";
}
}
console.log(sleepTimer(20));
//In this function, the boolean is playing in the background--the "if (time >= 9)" is asking:
//is it true that the time is larger than 9, or is it false? In either scenario, the interpreter
//returns the if statement with either true or false, and either returns Wake UP! or does not.
//4. Arrays
//Arrays are collections in javascript. It's like a variable except it can hold multiple
//values, which makes it very useful. An array is declared with square brackets, like so:
var myCoffeeMachines = ["Turkish coffee pot", "Moka Bialetti", "Crappy automatic espresso machine", "pour-over drip coffee", "trad Mr. Coffee Junker", "french press", "aeropress"];
//Arrays can store more than just strings, though--they can store nearly anything--objects, functions, numbers, booleans, etc.
var thingsOnMars = ["Turkish coffee pot", 1024, "Ziggy Stardust", (function ziggy(planet) {}, "Mars")];
//Behind the scenes, the items in an array are indexed starting at 0. So item one is 0, item two is 1, etc.
//Arrays can be accessed in a great many ways. The basic format is the array name and then,
//immediately after with no space, a square bracket containing the index number you wish
//to access in the array. Like so:
console.log("I really want a" + " " + thingsOnMars[0]);
//Arrays are technically objects. They're different in that the key/values of an object are not indexed
//like in an array, and are not in any particular order, whereas an array's data items are in an order.
//5. Object
//An object in javascript is a variable that, like an array, can contain more than one value. An object
//is more versatile, though. The format for storing things in objects is as a key/value pair--the key is
//the placeholder name for accessing the value. The value can be nearly anything. This is how objects are
//versatile--an object can be a function, or can simply store key/value pairs which, in
//turn, can store strings, or numbers, or a function. An object can INCLUDE a function, too, although while
//a function is inside an object it is called a method.
//You define an object
//6. Function
//Functions are like programs within a program--they are instructions that you are placeholding (technically, declaring)
//into the program to be executed, or invoked, when desired. Functions can be declared in two ways, and it definitely matters
//which way you choose, which will be explained underneath this example.
timoleon();
//timoleonTest();
var timoleonTest = function timoleonTest(street) {
console.log("These words will never see the light of day");
};
function timoleon(street) {
console.log("These words will! :)");
}
//The first function above is stated in literal notation and it will not be "hoisted." This means the javascript interpreter will
//have to wait until it reaches the line with that function's declaration in order to execute the function...so if you
//call the function before you declare it, as in this case, it will return an error. The second function, however,
//would work just fine even if you invoke it before declaring it. When a function, or anything, is "hoisted," this means that it doesn't matter
//on what line the thing exists within your program, it will be brought to the top and the moment the program begins it will be a factor.
//You can also declare an anonymous function, that is, a function with no name, like so:
// function (manWith, noName) {return manWith * noName}
//An anonymous function would usually be used in association with something else, inside of a variable, for example, or in an array--
//otherwise it wouldn't be so easy to call.
//Functions can be seen as the "verbs" of javascript, if variables are the "nouns"--they are the action, variables are the tools
//with which the action is taken.
//7. undefined
//Undefined means exactly what it is called--it is a variable that is undefined. This can either be literally defined as undefined:
// var meaningOfLife = undefined;
//or it can be undefined as in the variable was simply never given a definition, as such:
var meaningOfLice;
console.log(meaningOfLice);
//Undefined will often be seen as a returned error if code was messed up somewhere along the way, as often simple syntax errors can lead
//to a lack of definition of something like a variable or a function being called.
//8. null
//Null in javascript is nothing. I don't quite know how to use it any different than undefined, to be honest, but upon doing more research,
//it appears that null was not meant to return "object" when doing a typeof, but it does, and
//that this is considered a javascript bug. Some describe null as being where it is known that something exists but
//the value of that thing is unknown. You can clear a previously defined object by defining it as null.
//9. NaN
//Nan means Not-A-Number, and is not often used in a program, except perhaps when testing if a data type is a number,
//using NaN in the isNaN function, like so:
isNaN("Aurororororora Borealis");
isNaN(["degustibus", "non", "est", "disputandum"]);
isNaN(789838043);
//10. Google Infinity and -Infinity
//Infinity is shown when a number exceeds the maximum number of floating point numbers, and likewise,
//-infinity is shown when a negative number exceeds the same.
//The Infinity property appears according to research to be unlikely to be used in a program except, for example,
//to counteract the possibility of a number going that far and move on with your program's control flow,
//exculpating problems before they occur.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment