Fundamentals of JavaScript Code
Master Essential JavaScript Concepts and Syntax
Core JavaScript Concepts You'll Learn
Methods & Arguments
Learn how JavaScript methods work as actions that perform tasks. Understand how to pass data through arguments to control method behavior.
Variables & Data Types
Master variable declaration, assignment, and understand the differences between strings, numbers, and booleans in JavaScript.
Console & Debugging
Use Chrome DevTools Console for testing code, viewing output, and identifying errors in your JavaScript programs.
Setting Up Your Development Environment
Launch Code Editor
Open Visual Studio Code or your preferred code editor. Close any existing files to start fresh.
Open Project Folder
Navigate to JavaScript-Fundamentals folder in Desktop > Class Files > JavaScript Class and open it in your editor.
Preview in Chrome
Open index.html in Chrome browser using Option-B (Mac) or ALT-B (Windows) if using VS Code with browser extension.
Save the file and reload the page in your browser.
You should see a dialog displaying Hello. Notice the text appears without quotes, even though quotes were present in your code. This distinction between code syntax and output display is fundamental to understanding JavaScript.
Click OK to close the alert.
Methods vs Arguments
| Feature | Methods | Arguments |
|---|---|---|
| Definition | Actions that perform tasks | Data passed to methods |
| Role | The verbs of JavaScript | Information for the action |
| Syntax | methodName() | Inside parentheses |
| Example | alert() | 'Hello' |
Accessing Chrome DevTools Console
Right-Click Page
CTRL-click (Mac) or Right-click (Windows) on any webpage to open the context menu.
Choose Inspect
Select 'Inspect' from the context menu to open Chrome DevTools panel.
Click Console Tab
Navigate to the Console tab at the top of the DevTools window to view JavaScript output and errors.
JavaScript is case-sensitive! Variables myvar, myVar, Myvar, and MYVAR are all completely different. Pay close attention to capitalization when naming and using variables.
Variable Declaration: let vs var
| Feature | let | var |
|---|---|---|
| Modern Standard | Preferred syntax | Legacy syntax |
| Best Practice | Recommended | Discouraged |
| Usage | Use for new projects | Found in older code |
Rather than deleting this instructional code, we'll preserve it for reference by commenting it out. Add double slashes // to the beginning of each line:
// let myMessage = 'Hello';
// alert('myMessage');
// console.log('Hello from console');
TIP: Most modern code editors support rapid commenting. Select the lines and press Cmd–/ (Mac) or Ctrl–/ (Windows). This shortcut also works on single lines when your cursor is positioned anywhere within the line.
Save and reload the page in the browser.
Nothing executes because all the code is now commented out and ignored by the JavaScript interpreter.
JavaScript Syntax Essentials
Strings
Text data enclosed in single or double quotes. Quotes tell JavaScript to treat the content as literal characters rather than code.
Quotes
Either single or double quotes work for strings. Issues arise with nested quotes, so choose consistently throughout your code.
Semi-colons
Indicate the end of a JavaScript statement. Optional at line endings but recommended for clarity and best practices.
Code Comments
Addition vs Concatenation
| Feature | Numbers | Strings |
|---|---|---|
| Operation | Mathematical addition | String concatenation |
| Example Code | 2 + 2 | '2' + '2' |
| Result | 4 | 22 |
| Plus Sign Role | Addition operator | Concatenation operator |
Boolean variables store only true or false values (lowercase required). These are essential for conditional logic and decision-making in your programs.
Variable Naming Requirements
Use camelCase like grandTotal instead of grand total
Use day1 instead of 1day for valid syntax
Only dollar sign ($) and underscore (_) are allowed
Use reportConsole instead of console to prevent conflicts
Variable Naming Examples
| Feature | Poor Practice | Best Practice |
|---|---|---|
| Meaningless | let z = 10016 | let zipcode = 10016 |
| Too Verbose | fiveDigitZipCode | zipcode |
| Case Style | total_cost | totalCost |
| Constants | moonDiameter | MOON_DIAMETER |
Key Takeaways
