HCDD 411
1 / 15

JavaScript Syntax - Part 1

Md Touhidul Islam

Development Environment

Code Editor

Browser Developer Tools

💡 Pro Tip:
Press F12 or Ctrl+Shift+I (Windows) / Cmd+Option+I (Mac) to open browser developer tools and access the Console tab!
Open the Broswer Console and Try:

Why Do We Need JavaScript?

HTML/CSS

Define the content and layout

  • Structure
  • Styling
  • Static content

JavaScript

Handle changes and updates

  • Interactivity
  • Dynamic content
  • User events

JavaScript Code Locations

Four Ways to Add JavaScript: All examples below do the same thing - they display "Hello from JavaScript!"

1. Inside <head> Tag

<!DOCTYPE html>
<html>
<head>
    <script>
        function showMessage() {
            document.getElementById('demo').innerHTML = 'Hello from JavaScript!';
        }
    </script>
</head>
<body>
    <h1 id="demo">Original Text</h1>
    <button onclick="showMessage()">Click Me</button>
</body>
</html>

2. Inside <body> Tag

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <h1 id="demo">Original Text</h1>
    <button onclick="showMessage()">Click Me</button>
    
    <script>
        function showMessage() {
            document.getElementById('demo').innerHTML = 'Hello from JavaScript!';
        }
    </script>
</body>
</html>

3. External File (Recommended!)

HTML file:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <h1 id="demo">Original Text</h1>
    <button onclick="showMessage()">Click Me</button>
    
    <script src="script.js"></script>
</body>
</html>

script.js file:

function showMessage() {
    document.getElementById('demo').innerHTML = 'Hello from JavaScript!';
}

4. Inline JavaScript (Avoid!)

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <h1 id="demo">Original Text</h1>
    <button onclick="document.getElementById('demo').innerHTML = 'Hello from JavaScript!'">Click Me</button>
</body>
</html>

JavaScript Statements

Statement Terminators

EXPERIMENT: Both Styles Work
Output:

JavaScript Comments

Single Line

// comment

Multi Line

/* comment */
TRY IT: Practice Comments
Output:

JavaScript Variables

Declaring Variables

⚠️ Variable Naming Rules:
  • Case sensitive: myVarmyvar
  • Can contain: letters, numbers, $, _
  • Cannot start with numbers
  • Cannot use keywords (like if, for, etc.)
PLAYGROUND: Create Your Variables
Output:

JavaScript Data Types

Basic Data Types

💡 Use typeof: The typeof operator tells you the data type of any variable!
EXPLORE: Data Types & typeof
Output:

Data Type Conversion

To Number

Number()
parseInt()

To String

String()
.toString()

To Boolean

Boolean()
EXPERIMENT: Convert Data Types
Output:

JavaScript Operators

Operator Types

⚠️ Always use === and !== instead of == and !=
Strict equality checks both value AND type!
CALCULATOR: Test Operators
Output:

JavaScript Control Flow

Conditional Statements

Looping Statements

PRACTICE: Conditions & Loops
Output:

JavaScript Functions

Function Syntax

function functionName(param1, param2) {
  // code here
  return result;
}
BUILD: Create Functions
Output:
🎉 Congratulations!
You've completed JavaScript Syntax - Part 1! Keep experimenting with the code examples and build your own variations. The best way to learn is by doing!

Practice Problem 1: Variables & Math

🎯 Your Challenge:
Create a simple calculator that stores two numbers in variables, performs arithmetic operations, and displays the results.

Instructions:

💡 Example Output:
Sum: 15
Difference: 5
Product: 50
Division: 2
YOUR CODE: Complete the Calculator
Output:

Practice Problem 2: Age Checker

🎯 Your Challenge:
Write a program that checks someone's age and displays an appropriate message.

Instructions:

💡 Hint: Use comparison operators like <, >=, etc.
YOUR CODE: Build the Age Checker
Output:

Practice Problem 3: Build Your Own Function

🎯 Your Challenge:
Create a function that takes a name and an age, then returns a personalized greeting message.

Instructions:

💡 Remember: Use the return keyword to send back your message, and use + to combine strings!
YOUR CODE: Create the Greeting Function
Output: