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.
"let" vs "var"