-
Notifications
You must be signed in to change notification settings - Fork 4
Datatypes 2
When we are programming we must know what kind of data we are working with. Is our variable a Number, a String, an Object, something else? The programming word for kind of data is datatype. In Javascript there are 3 categories of datatype: simple, collection, and function.
Data is said to be of a simple type when it can only represent one thing at a time. The simple data types are:
A Number in javascript can be any number between 9,007,199,254,740,992 and -9,007,199,254,740,992. That's a lot of numbers! Numbers can also include a decimal part. To write a number we just type it out though we cannot use commas!
// some valid numbers
0;
12;
193874;
-100;
234.2304980;
-2345823059830193.13981798;
// some invalid numbers
//┌ Can't use commas!
10,000;
// ┌ Too many decimal points!
134.1309.7;
A String in Javascript can be any string of characters. It is called a string because javascript doesn't see words or meaning, it only sees a bunch of individual characters coming one after the other. There is no maximum length of a string though it is limited by the size of your computer's memory. To write a string we first start with a quote character, then we can have any character except that same quote character then we must end with the same quote character. There are 2 quote characters that Javascript lets you use: single quote -> ' and double quote -> ". All characters inside a string are significant this is including spaces, punctuation, and symbols. Remember: All characters in a string are significant.
// Some valid strings
// ┌ Double Quote ┌ Double Quote
"If you can't stand the heat,";
// ┌ Single Quote ┌ Single Quote
'stay out of Miami.';
// ┌ The smallest string: empty string!!
"";
// ┌ This string is 5 space characters
" ";
// ┌ Single Quote ┌ Single Quote
// ⇣ ┌ We can⇣put one quote type inside another
'We built a "lazer".'
// Some invalid strings
// ┌ Double Quote ┌ No closing quote!!
"I thought I told you;
// ┌ Double Quote ┌ Non matching closing quote!!
"That we wont stop'
// ┌ Double Quote ┌ Oops!!
"To take the wold hostage we've built a "Lazer"";
TODO: Escape Sequences
A Boolean in programming is a piece of data that can either be true or false and nothing else. There are no other possible values for a Boolean! This is extremely useful as many programming instructions work simply by checking whether a condition is either true or false. Such simplicity!
// Some valid Booleans
true;
false;
// Thats it!!! Nothing else is a valid Boolean.
// Some invalid booleans.
// This is a string, not a Boolean!!
"true";
// This is not valid, all letters must be lowercase!!
True;
In Javascript undefined means no value. When we declare a variable but don't assign it it is undefined. When we write a function that has no return value the function returns undefined.
// ┌ We declare a variable but don't assign it.
var someVariable;
someVariable === undefined; // This is true
// ┌ This function doesn't return anything.
function yay(){ 2 + 2; }
yay() === undefined; // This is also true
// ┌ We declare a person variable and assign it an empty object.
var person = {};
// ┌ Properties on that object that don't exist are undefined
person.name === undefined; // Also true
NaN
means Not a Number. Javascript gives us NaN
when we try to do math that doesn't make sense. Like if we try to divide a number by a string, we get NaN
. NaN
is not equal to anything, not even itself. We have to test for NaN
with isNaN()
13 / "spaghetti" // produces NaN
NaN === NaN // false
NaN !== NaN // True
isNaN(NaN) // True
// Be Careful!!
19847 / "" // Empty String converts to 0 so we get Infinity not NaN
Regular Expression Documentation
Regular Expressions are patterns used to search through and match characters on strings. For beginners they are difficult to use as they are an entire new language and way of thinking to learn on top of the learning you are already doing. Once you are comfortable enough to begin learning them they become an indispensable tool for getting the many jobs done quickly. And luckily Javascript's regular expressions have a syntax and behavior that is almost exactly the same to many other languages. A Sometimes we say RegEx instead of the full Regular Expression.
// ┌ Start of String
// | ┌ Maybe a Sign
// | | ┌ At least one non zero digit
// | | | ┌ Zero or more digits
// | | | | ┌ Maybe a decimal
// ⇣ ⇣ ⇣ ⇣ ⇣┌ End of string
var basic_number_matcher = /^[+-]?[1-9]+\d*(.\d+)?$/;
basic_number_matcher.test("-1234.987") // true
basic_number_matcher.test("something else") // false
basic_number_matcher.test("-1234.abc") // false
##Null Null Documentation
null is a datatype very similar to undefined
. The difference between null
and undefined
is mostly just that they are different words. null
is usually only set by a programmer, every so often something built into the language will return null
though. null
can be used when you want to explicitly mark a variable as containing nothing though I honestly can't think of a good reason to ever use null. :) null
is also associated with a bug in Javascript. typeof null
is supposed to return null
, but it returns object
. This bug has to stay for historical reasons. My recommendation is to not use null
but you will see others use it and must test for it. Luckily you can use === null
to test.
var a = null;
a === null; //true
typeof null === null; // false, language bug, you're not crazy. :)
Coming Soon!
Coming Soon!