Journal entries

    Week 1 Notes

    Published, February 10th, 2024

    What are compiled and interpreted languages and which is JavaScript?

    • Compiled languages - Source code is compiled into machine code by the compiler before its excuted
      Advantage: Faster execution speed, code is directly executed by the machine
      Disadvantages: Compilation is an additional step, and debugging can be more difficult as errors are found when compiling

    • Interpreted Languages (JavaScript) - Source code is executed line by line
      Advantage: Easier to test and debug, you can modify the code on the fly without needing to recompile
      Disadvantage: Slower execution since code is translated on the fly]

    Three common uses for JavaScript

    • Check form values
      Change appearance of HTML elements (e.g. show/hide)
      Modify the HTML document structure

    Using the Chrome developer tools

    • How can you find the page elements and style?
      - rick click anywhere on the website and select "inspect element" This will open a window, in the developer tools and highlight what elements you are viewing when hovering over code. Along with this, it shows its associated styles and the console log.

    • how do you inspect the different files making up a web page?
      - Open the Developer Tools panel. This should show a tab named "Sources" of all static files that compose your web page, such as HTML, CSS, JavaScript, images, and so on. Every file has a downward triangle on its left side. Click on it to open the file contents.

    • how do you inspect console outputs of JavaScript code?
      - Open up Developer Tools, and find the ‘Console’ panel which will show you those log messages being written, and other errors and outputs from the JavaScript running on your page. You can also go the other way, where you can enter any kind of JavaScript right in the console and execute it.


    What is JavaScript?


    • JavaScript is a high-level, interpreted programming language primarily used to create and control dynamic website content.

    What is it used for in the browser?


    • It’s used to add real-time interactivity and dynamism to webpages, which can manipulate the markup or presentation layer of the web – with or without HTML and CSS – for example: add events add animation implement games displaying dynamic data add anything interactively.

    Declarative vs. Procedural:


    • Declarative Programming: Concerned with the ‘what’ of the program without delving into the ‘how’ (eg, HTML, SQL).

    • Declarative Programming: Write step-by-step instructions that the program follows to achieve a certain state (eg, JavaScript, C)

    Compiled vs Interpreted:


    • Compiled Languages: Translated into machine code before execution (e.g., C, C++).

    • Interpreted Languages: Executed line-by-line by an interpreter at runtime (e.g., JavaScript, Python).

    Strongly vs. Weakly Typed:


    • Strong: variables are bound to data types.

    • Weak: Variables can hold values of any type.

    What are weakly and strongly typed languages and which is Javascript?

    • Weakly typed (JavaScrpt) - This is more flexible and allows for type conversion.
      Advantage: you can write quick and flexible code easily as it automatically handles conversion.
      Disadvantage: it can cause bugs and strange coding behaviours.

    • Strongly typed - Types are strictly enforced and implicit type conversions are limitedf or not allowed. Variables are expected to have a specific types.
      Advantage: Helps catch errors at compile time or runtime, leading to more predictable and reliable code
      Disadvantage: Can require more code to handle type conversions and might be less flexible for quick prototyping

    Two ways to activate the developer tools in chrome.


    • Click the three vertical dots (menu) in the upper-right corner of the Chrome browser.

    • Navigate to "More tools" and select "Developer tools".

    • Or you can press Ctrl+Shift+I

    • how do you debug JavaScript code in a page?
      Setting Breakpoints:
      Using the Debugger:
      - Once a breakpoint is hit, the execution will pause, and you can inspect the current state of the application.
      - Use the "Scope" and "Watch" sections to view variables and their values.
      - Use the controls at the top of the Sources panel (play, pause, step over, step into, step out) to navigate through the code.

    Week 2 Notes

    Published, February 17th, 2024

    Why should you always use a semi-colon in JavaScript


    • Despite the semi-colon being optional it should always be used in JavaScript as using them consistently helps prevent bugs, improves code clarity, and ensures that your code behaves as expected, particularly when minifying or concatenating scripts.


    What are statements?


    • Instructions that perform actions (e.g. "let x = 10;")

    What are comments used for?


    • Used to annotate code and improve readability.
      Single-line comment: "// this is a comment".
      Multi-line comment: "/* This is a multi-line comment */"

    Variables and Scopes:


    • Global Scope:Variables defined outside any function, accessible anywhere in the code.
      Function Scope:Variables defined within a function, accessible only within that function.
      Block Scope:Block Scope: Variables defined within a block {}, accessible only within that block (e.g., let and const).

    Reserved Keywords:


    • Keywords that cannot be used as variable names include:
      break, case, catch, class, const, continue, debugger, default, delete, do, else, export, extends, finally, for, function, if, import, in, instanceof, let, new, return, super, switch, this, throw, try, typeof, var, void, while, with, yield.

    Data Types:


    • Primitive types: Number, String, Boolean, Null, Undefined, BigInt.
      Reference types: Object, Array.

    Arithmetic Operators:


    • Used for mathematical operations: +, -, *, /, %.

    Increment (++) and Decrement (--) Operators:


    • Increment: x++ or ++x (increases value by 1).
      Decrement: x-- or --x (decreases value by 1).

    Operator Precedence:


    • Determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated first (e.g., * has higher precedence than +).

    String to Number Conversion:


    • parseInt("123") converts to an integer.
      parseFloat("123.45") converts to a floating-point number.
      Number("123") converts to a number.

    Determining Types:


    • Use typeof operator: typeof 123 returns "number", typeof "Hello" returns "string".

    Week 3 Notes

    Published, February 24th, 2024

    JavaScript variables can be declared with var and let. The difference is:


    • Scope: "var" is function scoped and "let" is block scoped.

    • Hoisting: "var" and "let" are hoisted but "var" is initialized to undefined but let isn't initialized.

    • Re-declaration: "var" allows re-declaration within the same scope where "let" doesn't.

    Control Structures:


    • For Loop: Repeats a block of code a specified number of times.

    • While Loop: Repeats a block of code as long as a condition is true.

    • Do-While Loop: Executes a block of code once, then repeats as long as a condition is true.

    • Break: Exits a loop prematurely.

    • Switch: Switch: Selects one of many code blocks to execute.

    Operators and Expressions:


    • Logical Operators: && (AND), || (OR), ! (NOT)

    • Expressions: Code that evaluates to a value (e.g., 3 + 4).

    Code Example:


    Week 4 Notes

    Published, March 2nd, 2024

    Basics of Strings:


    • Strings are sequences of characters used to represent text.

    Week 5 Notes

    Published, March 9th, 2024

    How Arrays Work:


    • Arrays in JavaScript are ordered collections of items, which can be of any type (numbers, strings, objects, etc.). Each item in an array is called an element, and each element has a specific index, starting from 0.

    Array Properties and Methods:


    • length: Returns the number of elements in the array.

    • push(): Adds one or more elements to the end of the array.

    • pop(): Removes the last element from the array.

    • unshift(): Adds one or more elements to the beginning of the array.

    • shift(): Removes the first element from the array.

    • splice(): Adds/removes elements from the array.

    • sort(): Sorts the elements of the array.

    Declaring an Array:


    // Using array literal

    let fruits = ["apple", "banana", "cherry"];

    // Using Array constructor

    let fruits = new Array("apple", "banana", "cherry");

    Reading and Setting Elements:


    // Reading elements

    let firstFruit = fruits[0]; // "apple"

    // Setting elements

    fruits[1] = "blueberry"; // Changes "banana" to "blueberry"

    Looping over an Array:


    // Using a for loop

    for (let i = 0; i < fruits.length; i++) {

    console.log(fruits[i]);

    }

    Using arrays like Stacks:


    Basic Code Examples:


    Sorting Numeric Arrays:


    Converting Between Strings and Arrays:


    Advanced Array Methods:


    Week 6 Notes

    Published, March 16th, 2024

    Named vs Anonymous Functions


    • Named Functions: Functions that have a name.

    • Anonymous Functions: Functions without a name, are often used as arguments to other functions or assigned to variables.

    Function Parameters:


    • Functions can accept inputs called parameters. Parameters allow functions to operate with different values.

    • Functions can return values using the "return" statement. This allows them to produce output.

    Function Call vs References


    • Function Call: Executes the function.

    • Function Reference: Refers to the function itself, without executing it.

    Arrow Functions


    • Arrow functions provide a shorter syntax for writing functions. They also differ in behaviour, especially with "this".

    • Arrow functions do not have their own "this" value; they inherit "this" from the surrounding code context.

    Variable Scopes:


    Variable scope refers to the visibility of variables. JavaScript has two types of scope:


    • Global Scope: Variables declared outside any function are in the global scope and accessible from anywhere in the code.

    • Local Scope: Variables declared within a function are in the local scope and accessible only within that function.

    "let" vs "var"
    • var: Declares a variable that is function-scoped or globally-scoped (if declared outside a function). Variables declared with var are hoisted to the top of their scope and can be redeclared.

    • let: Declares a block-scoped variable. Variables declared with let are not hoisted to the top of their scope and cannot be redeclared in the same scope.

    Example Code:


    Week 7 Notes

    Published, March 23rd, 2024

    Accessing DOM Nodes:


    • DOM nodes can be accessed using various methods provided by the "document" object. Common methods include "getElementById", "getElementsByClassName", "getElementsByTagName", "querySelector", and "querySelectorAll".

    Advantages of "querySelector()" and "querySelectorAll()"

    • Simplicity: They allow you to use CSS selectors, making the code more readable and concise.

    • Versatility: They can select elements using any CSS selector, including class names, IDs, attribute selectors, etc.

    • Efficiency: They return a static NodeList or single Element, which can be faster than live HTMLCollection returned by other methods like getElementsByClassName.

    Accessing Parent, Sibling, and Child Nodes


    • Parent Node: Use the parentNode property to access the parent node of an element.

    • Sibling Nodes: Use properties like previousSibling, nextSibling, previousElementSibling, and nextElementSibling to access sibling nodes.

    • Child Nodes: Use properties like child nodes or methods like children to access child nodes.

    Modifying DOM Nodes:


    • DOM nodes can be modified using properties and methods such as "innerHTML", "textContent", "appendChild", "removeChild", "setAttribute", "classList", etc.

    Parameters for Accessing the DOM


    The DOM can be accessed:


    • During Page Load: The DOM can be accessed and modified as soon as the document is loaded.

    • After Page Load: Scripts can access and modify the DOM after the document has been fully loaded using events like DOMContentLoaded.

    • Asynchronously: Scripts can access and modify the DOM asynchronously after the document is loaded, for example, in response to user actions or AJAX requests.

    Example code:

    DEFINITIONS AND EXTRAS

    Last Updated, May 21st, 2024