Bo dinglesTM - -   Bright Lightbulb Graphic Ideas   - -

The Web Developer Bootcamp 2024

Colt Steele

Back to JavaScript Index Page


JavaScript Basics

JavaScript Introduction PDF

Primitive Type Variables

The Basic JavaScript Building Blocks. JavaScript has 7 primitive data types:

  • Numbers
    • Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
    • Modulus (%)
      • The modulus operator (%) returns the division remainder.
        • x = 5, y = 2 : z = x % y : z = 1 (remainder of 5 divided by 2)
      • *note: The "MOD" operator with 2 is a good way to find out if any number is ODD or EVEN.
        • if the mod operation returns 1, the number is ODD | 13 % 2 = 1 | 13 is an Odd number.
        • if the mod operation returns 0, the number is EVEN | 14 % 2 = 0 | 14 is an Even number.
    • Incrementing (++)
      • The increment operator (++) increments numbers.
        • x = 5 : y = x ++ : y = 6 (5 incremented by 1)
    • Decrementing (--)
      • The decrement operator (--) decrements numbers.
        • x = 5 : y = x -- : y = 4 (5 decremented by 1)
    • Shortcuts (+= -= *= /=)
      • Add, Subtract, Multiply, Divide by more than One at a time. Shortcut for operations.
        • x = 5 || x += 5 is the shorcut version for x = x + 5
        • This also works for -= | *= | /=
    • Powers **
      • The power operator (**) returns the power of a number.
        • x = 2 | y = 4 -- : 2 ** 4 = 16 : 2 to the 4th power returns 16 (2 * 2 * 2 * 2)
    • NaN: Not a Number
      • In JavaScript, NaN is a number that is not a legal number. Any mathematical operation involving NaN, returns NaN. There are five different types of operations that return NaN:
        • - Failed number conversion such as (e.g. 0 / 0)
        • - Math operation where the result is not a real number (e.g. Math.sqrt(-1))
        • - Indeterminate form (e.g. 0 * Infinity, 1 ** Infinity, Infinity / Infinity, Infinity - Infinity)
        • - A method or expression whose operand is or gets coerced to NaN (e.g. 7 ** NaN, 7 * "blabla") — this means NaN is contagious
        • - Other cases where an invalid value is to be represented as a number (e.g. an invalid Date new Date("blabla").getTime(), "".charCodeAt(1))
  • String
    • Strings are for storing text. A JavaScript string is zero or more characters written inside quotes. You can use single or double quotes. You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
  • Boolean
    • A JavaScript Boolean represents one of two values: True or False. Boolean(10 > 9) evaluates to True because 10 IS greater than 9. Boolean(10 > 11) evaluates to False because 10 IS NOT greater than 11.
  • Null
    • - The null keyword represents an intentionally defined absence of value. null is a primitive, although the typeof operator returns that null is an object. You might define a variable as null with the expectation that it reflects either a value assigned to it at some point in a script or an explicitly absent value. You can also assign the null value to an existing reference to clear a previous value.
    • - The intentional absence of any value
    • - Null MUST be assigned
  • Undefined
    • - A primitive value assigned to variables that have just been declared, or to the resulting value of an operation that doesn't return a meaningful value. For example, this can happen when you declare a function in a browser's developer console. A function explicitly returns undefined when its return statement returns no value.
    • - Variables that do NOT have an assigned value. (a variable that has been declared, but not assigned).
  • Bigint
    • JavaScript BigInt variables are used to store big integer values that are too big to be represented by a normal JavaScript Number. JavaScript integers are only accurate up to 15 digits. To create a BigInt, append n to the end of an integer or call BigInt()
  • Symbol
    • unknown at this time


PEMDAS

Order of Mathmatical Operations (PEMDAS)

  • Parentheses
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Suntraction


typeof()
  • The typeof operator returns the data type of a JavaScript variable. for example:
    • typeof "John" || Returns string
    • typeof 33 || Returns number
    • typeof true || Returns boolean
    • typeof x || Returns undefined

Comments
  • JavaScript has comments just like HTML and CSS. The format is // and there is NO closing comment tag.
  • // I am a javascript comment

Let vs Const

Let and Const are used to assign variables.

  • let assigns a data type to a variable, and can be changed
    • let x = 5 | x is now equal to 5
    • x can be changed because it was assigned with let | x += 5 | x now equals 10
  • const assigns a data type to a variable, and can NOT be changed
    • const x = 5 | x is now equal to 5
    • x can NOT be changed because it was assigned with const | x += 5 | returns a JavaScript error


Console.log - Alerts - Prompts

Colsole.log() allows for output to the screen. examples:

  • console.log('Hello World'); will output to the screen : Hello World
  • console.log(variablea + variableb); will output to the screen : sum variablea + variableb
  • console.log('I have ' + variable + ' eggs.'); will output to the screen : I have xx eggs


The alert() method displays an alert box with a message and an OK button. The alert() method is used when you want information to come through to the user. examples:

  • alert("Hello! I am an alert box!!"); will open a pop-out dialog saying "Hello! I am an alert box!!"


The prompt() method is similar to the alert() method in that it opens a pop-out box. The difference is that an alert() box is an input asking for the user to input some information. The prompt() method returns the input value if the user clicks "OK", otherwise it returns null. examples:

  • prompt("Please enter your name"); will open a pop-out dialog, asking for input to, "Please enter your name"


Back to Top