JavaScript Part 1
JavaScript Part 1
Deep Dive
When to use?
Analogue
- JavaScript in various domains:
- Imagery: JavaScript as magic sauce - Web Development: Interactivity
- Transforms static pages into interactive experiences - Server-Side Development: Backend with [Link]
- Click buttons, see real-time changes, no page reloads - Mobile App Development: React Native, Ionic
- Data Analysis: [Link] and data science
Summary
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
SETTING
DEVELOPMENT
ENVIRONMENT
GETTING STARTED
YOUR
FIRST
JS CODE
GETTING STARTED
WAYS OF INCLUDING
JS IN
HTML
GETTING STARTED
WAYS OF INCLUDING JS IN HTML
High-Level Explanation
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
WAYS OF INCLUDING JS IN HTML
- Include JavaScript in HTML: - Embedded JavaScript: Code within `<script>` tags, runs where located.
- `<script>` tag - External JavaScript with `async`: `<script async src="[Link]">`
- External `.js` file link - Downloads and executes immediately.
- Inline JavaScript in HTML elements - External JavaScript with `defer`: `<script defer src="[Link]">`
- `async` and `defer` attributes for external file control - Downloads, executes after HTML parsing.
- Inline JavaScript: Within HTML element attributes, e.g., `onclick`, `onload`.
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
WAYS OF INCLUDING JS IN HTML
- Include JavaScript in HTML: - Embedded JavaScript: Code within `<script>` tags, runs where located.
- `<script>` tag - External JavaScript with `async`: `<script async src="[Link]">`
- External `.js` file link - Downloads and executes immediately.
- Inline JavaScript in HTML elements - External JavaScript with `defer`: `<script defer src="[Link]">`
- `async` and `defer` attributes for external file control - Downloads, executes after HTML parsing.
- Inline JavaScript: Within HTML element attributes, e.g., `onclick`, `onload`.
Analogue
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
WAYS OF INCLUDING JS IN HTML
- Include JavaScript in HTML: - Embedded JavaScript: Code within `<script>` tags, runs where located.
- `<script>` tag - External JavaScript with `async`: `<script async src="[Link]">`
- External `.js` file link - Downloads and executes immediately.
- Inline JavaScript in HTML elements - External JavaScript with `defer`: `<script defer src="[Link]">`
- `async` and `defer` attributes for external file control - Downloads, executes after HTML parsing.
- Inline JavaScript: Within HTML element attributes, e.g., `onclick`, `onload`.
Analogue
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
WAYS OF INCLUDING JS IN HTML
- Include JavaScript in HTML: - Embedded JavaScript: Code within `<script>` tags, runs where located.
- `<script>` tag - External JavaScript with `async`: `<script async src="[Link]">`
- External `.js` file link - Downloads and executes immediately.
- Inline JavaScript in HTML elements - External JavaScript with `defer`: `<script defer src="[Link]">`
- `async` and `defer` attributes for external file control - Downloads, executes after HTML parsing.
- Inline JavaScript: Within HTML element attributes, e.g., `onclick`, `onload`.
Analogue
Important Rules
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
WAYS OF INCLUDING JS IN HTML
- Include JavaScript in HTML: - Embedded JavaScript: Code within `<script>` tags, runs where located.
- `<script>` tag - External JavaScript with `async`: `<script async src="[Link]">`
- External `.js` file link - Downloads and executes immediately.
- Inline JavaScript in HTML elements - External JavaScript with `defer`: `<script defer src="[Link]">`
- `async` and `defer` attributes for external file control - Downloads, executes after HTML parsing.
- Inline JavaScript: Within HTML element attributes, e.g., `onclick`, `onload`.
Analogue
Important Rules
Summary
- Embedded JavaScript: Minimal, demos, testing.
- External JavaScript with `async`: DOM-independent scripts. - JavaScript inclusion methods vary with use-cases.
- External JavaScript with `defer`: For fully-loaded DOM. - `async` and `defer` usage controls script execution, enhances performance.
- Inline JavaScript: Avoid for cleaner alternatives.
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
JS
VARIABLES
OVERVIEW
VARIABLES & OPERATORS
JS VARIABLES OVERVIEW
Value/Items
Syntax
let/const
var
keyword
Variable Name/Container
- JavaScript variables: Data containers for code manipulation. - JavaScript variables: Hold data values or references.
- Keywords: `var`, `let`, `const`.
- `var`: Function-scoped, redeclarable, updatable.
- `let`: Block-scoped, updatable, not redeclarable.
Analogue - `const`: Block-scoped, not updatable, not redeclarable.
Important Rules
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
DIFFERENCES BETWEEN
Analogue
When to use?
- Imagery: Lunchboxes representing variables
- `var`: Opens, spills unpredictably. - `var`: Less common, function-scoped.
- `let`: Smart, stays shut, changeable. - `let`: Value changes, block-specific.
- `const`: Locked, unchangeable, choose wisely. - `const`: Value constant, consistent.
- `var`: Avoid, prefer `let` and `const`. - `var`, `let`, and `const`: Variable declaration.
- `let`: Changeable variables. - `var`: Function-scoped, older, avoid.
- `const`: Immutable variables for clarity. - `let`: Changeable, block-scoped.
- `const`: Immutable, block-scoped.
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
DATA
TYPES
VARIABLES & OPERATORS
DATA TYPES
Value/Items
Syntax
let/const
var
keyword
Variable Name/Container
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
WHAT ARE DATA TYPES
- JavaScript data types: Containers for storing items - JavaScript: Dynamically typed
- Hold numbers, words, lists, etc. - Two data type categories:
- Primitive Types: Immutable, single value
- Number, String, Boolean, Undefined, Null, BigInt, Symbol
- Non-Primitive Types: Mutable, multiple values
- Objects, Arrays
Analogue
When to use?
- Primitive Types: Solo cups for individual drinks
- Non-Primitive Types: Punch bowls for mixing and customization - Primitive Types: Single, unchangeable values (e.g., user info, settings)
- Non-Primitive Types: Complex data (e.g., book list, user account configs)
Important Rules
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
JS
OPERATORS
OVERVIEW
VARIABLES & OPERATORS
OPERATORS IN JAVASCRIPT OVERVIEW
- JavaScript operators: Symbols for operations JavaScript operators are categorized into:
- Tools in math-like equations - Arithmetic Operators: Math calculations
- Compare, calculate, assign, and more - Comparison Operators: Value comparisons
- Logical Operators: Boolean logic
- Assignment Operators: Variable value setting
- Unary Operators: Single operand operations
- Ternary Operator: Short conditional operation
- Bitwise Operators: Binary representation level operations
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
OPERATORS IN JAVASCRIPT OVERVIEW
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
ARITHMETIC
OPERATORS
- JavaScript arithmetic operators: Perform math operations - Addition (`+`) : Add two numbers.
- Examples: Addition, subtraction, multiplication, division - Subtraction (`-`) : Subtract right from left operand.
- Multiplication (`*`) : Multiply two numbers.
- Division (`/`) : Divide left by right operand.
- Modulus (`%`) : Get remainder after division.
- Increment (`++`) : Increase value by one.
- Decrement (`--`) : Decrease value by one.
Analogue
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
COMPARISON
OPERATORS
- JavaScript comparison operators: Compare values, yield true/false - Equal to (`==`): Check equal values after type conversion.
- Determine truth of comparisons - Not equal to (`!=`): Check unequal values after type conversion.
- Strictly equal (`===`): Verify equal values and types.
- Strictly not equal (`!==`): Confirm unequal values or types.
Important Rules
When to use?
- Favor `===` and `!==` to prevent type conversion surprises. - Equal to (`==`) and Not equal to (`!=`) for type-agnostic comparisons.
- Ensure appropriate data types for comparisons. - Strict comparisons (`===`, `!==`) for type-aware comparisons, preventing type
conversion surprises.
Summary
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
LOGICAL
OPERATORS
- JavaScript logical operators: Perform logic on values There are mainly three logical operators:
- Determine relationships between conditions - AND (`&&`): Returns true if both operands are true.
- OR (`||`): Returns true if at least one operand is true.
- NOT (`!`): Returns true if the operand is false and false if it’s true.
Analogue
Summary
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
ASSIGNMENT
OPERATORS
- JavaScript assignment operators: Assign values to variables - `=`: Assigns a value to a variable.
- Used for setting variable values - `+=`: Adds right value to the left variable.
- `-=`: Subtracts right value from the left variable.
- `*=`: Multiplies left variable by right value.
- `/=`: Divides left variable by right value.
- `%=`: Finds modulus of left variable and right value.
Analogue
When to use?
- Assignment operators: Combine calculation and assignment.
- Utilize assignment operators for concise, readable code.
- Shortcut math for updating variable values in one step.
- Streamline variable calculations and assignments.
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]
TERNARY
OPERATORS
- Ternary operator: Shortened if-else statement - Ternary operator parts: Condition, Value if true, Value if false.
- Checks a condition - Format: `condition ? valueIfTrue : valueIfFalse;`
- Returns one value if true, another if false
Analogue
When to use?
- Analogy: Ternary operator as a decision maker
- Ternary operator ideal for simple, one-line decisions.
- Condition: "Is it raining?"
- Use for straightforward conditions and value choices.
- Value if true: "Take an umbrella"
- Best for concise, readable code.
- Value if false: "Take sunglasses"
Summary
MASYNCTECH M A S Y N C T E C H M A S Y NwCwT w
EC.m
H a s y n c t e c h . c o m [Link]