Skip to content

Latest commit

 

History

History

02-variables-data-types-operators

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Variables

  • Variables are used to store data

  • Variables are only visible (scope) inside of the functions where they are declared

  • Before using a variable we have to:

    • Declare the variable (with the sentence var)
    • Initialize the variable (at the time of the declaration or after)
var a = 1;
var b;
b = 2;
  • Variables are Case Sensitive
var case_matters = 'lower';
var CASE_MATTERS = 'upper';
case_matters
CASE_MATTERS

How to check the existance of a variable?

  • Bad way

    >>> var result = '';
    >>> if (somevar){result = 'yes';}
    somevar is not defined
    >>> result;
    ""

It generates a warning and that ‘somevar’ returns FALSE it doesn't mean that the variable is not defined

  • Right way

    >>> var somevar;
    >>> if (typeof somevar !== "undefined"){result = 'yes';}
    >>> result;
    ""
    >>> somevar = 123;
    >>> if (typeof somevar !== "undefined"){result = 'yes';}
    >>> result;
    "yes"

If the variable is defined and it has some value, its data type will always be different from undefined

Primitives and Data Types

  • Any value used in Javascript is of a certain type. In Javascript the following primitive data types exist:

    • Number: They can contain integer, float), hexadecimals, octals, exponentials and the special NaN and Infinity numbers
    • String: Any number of characters between quotes
    • Boolean: It can be true or false
    • Undefined: It's a data type with only one value: undefined Javascript returns this value when a variable doesn't exist or is not initialized.
    • Null: Another data type with only one value: null We can use it to initialize a variable wirh an empty value.
  • Any value that doesn't belong to any of these 5 primitive type of data is an object

  • So, data types in Javascript can be:

    • Primitives (These 5 types)
    • No primitives (Objects)
  • Although there is the operator typeof that returns the data type, it's better to use Object.prototype.toString

>>> typeof([1,2,3])
"object"
>>> Object.prototype.toString.call([1,2,3])
"[object Array]"
>>> typeof(function(){})
"function"
>>> Object.prototype.toString.call(function(){})
"[object Function]"
>>> typeof(new Date())
"object"
>>> Object.prototype.toString.call(new Date())
"[object Date]"
>>> typeof(27)
"number"
>>> Object.prototype.toString.call(27)
"[object Number]"
  • There's the special value NaN (Not a Number) that we obtain when we try to perform an operation that expects numbers but something fails.
>>> var a = 10 * f;
>>> a
NaN

Operators (Arithmetical, Logical and Comparison)

  • Operators can take one or two values (or variables), then they perform an operation, and then they return a value

  • The assign values to variables we use the assignment operator =

var a = 1;

Arithmetical Operators

  • The basic arithmetical operators are:

    + Addition

    >>> 1 + 2;
    3

    - Substraction

    >>> 99.99 - 11;
    88.99

    * Multiplication

    >>> 2 * 3;
    6

    / Division

    >>> 6 / 4;
    1.5

    % Modulo

    The remainder of a division

    >>> 6 % 3;
    0
    >>> 5 % 3;
    2

    We can use the remainder operator to determine, for example, if a number is odd (% = 1) or even (% = 0) by dividing it by 2.

    >>> 4 % 2;
    0  // even
    >>> 5 % 2;
    1  // odd

    ++ Increment by 1

    Post-increment returns the original value (return) and then increases the value by 1.

    >>> var a = 123; var b = a++;
    >>> b;
    123
    >>> a;
    124

    Pre-increment increases the value by 1 and then returns (return) the value (already increased).

    >>> var a = 123; var b = ++a;
    >>> b;
    124
    >>> a;
    124

    -- Decrement by 1

    Post-decrement returns the original value (return) and then decreases the value by 1.

    >>> var a = 123; var b = a--;
    >>> b;
    123
    >>> a;
    122

    Pre-incremento decreases the value by 1 and then returns (return) the new value (already decreased).

    >>> var a = 123; var b = --a;
    >>> b;
    122
    >>> a;
    122
  • There are also combined operators

>>> var a = 5;
>>> a += 3;
8

## Logical Operators

  • Logical Operators are:

    • ! → logical NOT (negation)

    • && → logical AND

    • || → logical OR

>>> var b = !true;
>>> b; 
false
  • Double negation returns the original value
>>> var b = !!true;
>>> b; 
true
  • The possible operations and their results are:
Operation Result
true && true true
true && false false
false && true false
false && false false
`true
`true
`false
`false

## Comparison Operators

  • Comparison Operators are:

    == Equality

    Returns true when both operands are the same. The operands are converted to the same type of data before comparing them

    >>> 1 == 1;
    true
    >>> 1 == 2;
    false
    >>> 1 == '1';
    true

    === Equality and Type

    Returns true when both operands are equal AND when they have the same data type. It's usually better and safer using this equality comparison (there are no uncontrolled conversions behind the curtains)

    >>> 1 === '1';
    false
    >>> 1 === 1;
    true

    != Non-equality

    Returns true when both operands are NOT equal (after a type conversion)

    >>> 1 != 1;
    false
    >>> 1 != '1';
    false
    >>> 1 != 2;
    true

    !== Non-equality without type conversion

    Returns true when bot operands are not equal AND when they have different data type

    >>> 1 !== 1;
    false
    >>> 1 !== '1';
    true

    > Greater than

    Returns true if the operand in the left is greater than the operand in the right

    >>> 1 > 1;
    false
    >>> 33 > 22;
    true

    >= Greater or equal than

    Returns true if the operand in the left is greater or equal than the operand in the right

    >>> 1 >= 1;
    true

    < Less than

    Returns true if the operand in the left is less than the operand in the right

    >>> 1 < 1;
    false
    >>> 1 < 2;
    true

    <= Less or equal than

    Returns true if the operand in the left is less or equal than the operand in the right

    >>> 1 <= 1;
    true
    >>> 1 <= 2;
    true

Conversions

  • If we use a number between quotes (string) in an arithmetical operation, Javascript converts it in a number
>>> var s = "100"; typeof s; 
"string" 
>>> s = s * 1; 
100 
>>> typeof s; 
"number"
  • ¡Warning! undefined and null return different things when converted to number
>>> 1*undefined 
NaN
>>> 1*null 
0
  • If we use true or false between quotes Javascript converts them to string
>>> var b = "true"; typeof b; 
"string"
  • Double negation !! is a quick way to convert any value to its correspondent boolean.

    >>> !!0
    false
    >>> !!1
    true
    >>> !!""
    false
    >>> !!"hola"
    true
    >>> !!undefined
    false
    >>> !!null
    false

    Applying this techniqe we could check how any value converted to boolean is true but:

    • ""
    • null
    • undefined
    • 0
    • NaN
    • false

    Because of this, these values are also called Falsy Values