JavaScript-Intro-JS 🙀
Source: Intro to JavaScript - Udacity Front End Web Development Nanodegree
- a. What is JavaScript?
- b. The JavaScript Console
- c. Developer Tools on Different Browsers
- d- Console.log
- HTML and CSS are
markup languages, that describe and define elements within a document. - JavaScript is a
programming language, used to communicate instructions to a machine to:- control the behavior of a machine
- express algorithms.
The JavaScript Console is a development tools to iterate, debug and profile JavaScript code directly into the web browser.
"Diana"
alert("Hello "Diana"! How are you?!");
This creates a JavaScript alert box in the web browser, on the current web page.
Every modern web browser includes its own set of developer tools:
- The Chrome DevTools are a set of web authoring and debugging tools built into Google Chrome.
- Use the DevTools to
iterate,debugandprofilea site. - Open Chrome DevTools:
right-clickon any page element, selectInspect.- open the
Chrome settings menuin the top-right corner of your browser window and selectMore Tools > Developer Tools. - Use shortcuts:
Command + Option + i(Mac) ORCtrl + Shift + i/F12(Windows/Linux).
- Firefox Developer Tools allow to examine, edit, and debug HTML, CSS, and JavaScript on the desktop and on mobile.
- Download a version of Firefox called
Firefox Developer Editiontailored for developers, featuring the latest Firefox features and experimental developer tools. - Open Firefox Developer Tools:
right-clickon any page element, selectInspect.- open the
Firefox settings menuin the top-right corner of your browser window and selectDeveloper. - Use shortcuts:
Command + Option + i(Mac) ORCtrl + Shift + i/F12(Windows/Linux).
- The features vary between versions, but starting at Internet Explorer 8 remain pretty consistent.
- Internet Explorer 8
- Internet Explorer 9
- Internet Explorer 10
- Internet Explorer 11
- Open Internet Explorer Developer Tools:
F12
- Microsoft Edge introduces improvements to the F12 developer tools in Internet Explorer.
- The new tools are built in
TypeScript, and are always running, so no reloads are required. - F12 developer tools documentation available on GitHub.
- Open Microsoft Edge Developer Tools:
F12
For any Mac users, Safari includes Web Inspector, a powerful tool that makes it easy to modify, debug, and optimize a website for peak performance and compatibility on both platforms.
- Safari's Web Development Tools:
- Enable the Develop menu in
Safari’s Advanced preferences. Thenright-clickon any page element, selectInspect Element. - Use shortcuts:
Command + Option + i(Mac).
- Enable the Develop menu in
- Fast, lean and powerful, Opera comes pre-packed with a fully-featured suite of developer tools.
- Opera Dragonfly is designed to make your job easier.
- Open Opera Dragonfly:
right-clickon any page element, selectInspect Element.- Use shortcuts:
Command + Option + i(Mac) ORCtrl + Shift + i/F12(Windows/Linux).
console.log is used to display content to the JavaScript console.
Perform the log action on the debugging console:
- Run the following code in the console:
console.log("hiya friend!");
-
Prints: "hiya friend!"
-
The message logged is "hiya friend!".
-
hiya friend! is a
string(=a sequence of characters). -
Write a block of JavaScript code that
loopsthrough the numbers 0 through 9 and prints them out to the console:
for (var i = 0; i < 10; i++) {
console.log(i);
}
- Prints: "0 1 2 3 4 5 6 7 8 9"
- This means: any code written inside the curly brackets
{...}will be repeated 10 times. - In this case,
console.logis printing out the value ofieach time the loop runs.
How to use the console as a sandbox to test a new line of JavaScript in the browser?
- Open this site in Dev Tools.
- Paste the following code:
document.getElementsByTagName("h1")[0].style.color = "#ff0000"; - What happened when you ran that line of code in the JavaScript console?
- The heading changed to RED.
- Styling elements on the page is great, can also be don by modifying the CSS.
What makes JavaScript so special in this case?
- Refresh the page, then paste this line of code in the JavaScript console.
document.body.addEventListener('click', function () { var myParent = document.getElementById("Banner"); var myImage = document.createElement("img"); myImage.src = 'https://thecatapi.com/api/images/get?format=src&type=gif'; myParent.appendChild(myImage); myImage.style.marginLeft = "160px"; }); - When you click, an image (cat gif)is added to the webpage 🐱! An image is added to the page.
- Chrome Dev Tools Keyboard Shortcuts
- Google Chrome DevTools
- Mozilla Firefox
- Internet Explorer 8
- Internet Explorer 9
- Internet Explorer 10
- Internet Explorer 11
- Microsoft Edge
- Safari Web Inspector
- Opera Dragonfly
- All mayor web browsers come with build-in JavaScript Engines
- This allows browsers to
runandexecute Javascript code - The JavaScript Console allows to use
strings, andexecute Javascript code linesin real time directly in the browser. - With JavaScript in JavaScript Console, you can add styles and animations to a web page.