Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* DATATYPES:
- Different types of values that Javascript recognizes. There are two types
- of datatypes; simple and complex.
- Simple datatypes are values that DO NOT aggregate or collect other values;
- they are just one value. This means they're immutable. When they are are
- assigned or passed they are copied from one variable to the next by their
- values.
- Examples of Simple Datatypes:
- 1.String
- 2.Number
- 3.Boolean
- 4.Undefined
- 5.Null
- 6.NaN (Not a Number)
- */
- //Strings are text values wrapped in quotation marks
- 'use strict';
- var myString = 'Hi There!';
- //Numbers are numberic values
- var num = 10;
- //Booleans are values that represent true or false
- var myBoolean = true;
- console.log(myBoolean); //prints true value
- myBoolean = false;
- console.log(myBoolean); //prints false value
- //Undefined is a datatype that represents the non-existence of a value
- //This is typically assigned by Javascript
- var notDeclaredVar;
- console.log(notDeclaredVar); //prints undefined
- /* Null is another datatype that represents the non-existence of a value
- but it is intentionally set by the programmer to be 'nothing'.
- */
- var nullValue = null;
- console.log(nullValue);
- //NaN (Not a Number) is value the cannot be evaluated to be a numberic value.
- //It gets returned when mathmatical functions fail.
- /* Infinity is a datatype that represents a value that is greater
- than any other number; anyhting multiplied by Infinity is Infinity
- and anything divided by Infinity is 0.
- -Infinity is a datatype that represents an infinite value but acts
- differently from Posiitve Infinity.
- Any positive number including Infinity multiplied by -Infinity results in
- -Infinity; any negative number including -Infinity results in Infinity.
- 1.Zero multiplied by -Infinity is NaN.
- 2.NaN multiplied by -Infinity is NaN.
- 3.-Infinity, divided by any negative value except -Infinity, is Infinity.
- 4.-Infinity, divided by any positive value except Inifinity, is -Infinity.
- 5.-Infinity, divided by either -Infinity or Infinity, is NaN.
- 6.Any number divided by -Infinity is zero.
- */
- /*Complex datatypes include objects, arrays, and functions. They're complex
- because they are mutable which means they DO collect other values; therefore
- have an indefinite size.
- When they are assigned to variables or passed through function
- they are copied by reference because they can be so large in size.
- */
- //Example:
- var myObject = {
- name: 'value',
- key: 'value'
- };
- console.log(myObject); //prints the object with two key value pairs
- var myArray = [1, 2, 3, 4, 5];
- console.log(myArray); // prints the array that contains the numbers 1-5
- function doSomething(a) {
- a += 1;
- }
- var z = 1;
- doSomething(z);
- console.log(z); //prints 1 not 2, the value of variable z is copied through
- // calling the function doSomething().
- /* Many datatypes have different properties and methods attached to them.
- properties (things they're made of) / methods (things they can do).
- For example an array has a length property that tells how many values
- are stored in an array we can use the .length method access this information.
- */
- //Example:
- console.log(myArray.length); //prints the number 5 because there 5 values
- //held in that array.
- /* Functions are blocks of code that hold 'instructions' telling the
- computer what to do and how to get it done. They are declared with
- the keyword function followed by a set of parentheses and curly braces.
- The instructions for what to do go inside the curly braces.
- Sometimes functions take parameters/arguments; these are values or
- 'information' that the function needs in order to get the job done.
- Parameters are basically named variables while arguements are the actual
- values used.
- */
- //Example:
- /*This function passes two numbers as arguments, adds them together
- and then returns the sum.
- */
- function add(numOne, numTwo) {
- var sum = numOne + numTwo;
- return sum;
- }
- console.log(add(1, 2)); //prints the number 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement