JavaScript: Data Types and Variables (Part 1)
JavaScript 101: Your First Step into the World of Code (Part 1)
What You'll Learn in This Session:
Why we need JavaScript and where it's used. How to write and see the output of your first program. The basic "ingredients" of code: Primitive Data Types. How to store information using Variables (var, let, and const). The do's and don'ts of naming your variables.
Why Do We Even Need JavaScript?
HTML (HyperText Markup Language) is the frame and structure. It builds the walls, the roof, and the rooms. It's the skeleton of a webpage. CSS (Cascading Style Sheets) is the paint and decoration. It adds colors, sets the fonts, and arranges the furniture. It makes the webpage look good. JavaScript (JS) is the electricity and plumbing. It makes the house functional. It powers the lights when you flip a switch, makes the water run when you turn a tap, and opens the garage door when you press a button.
Showing a pop-up when you click a button. Validating a form to make sure you entered a proper email address. Loading new posts on a social media feed without reloading the page. Creating complex animations and browser-based games.
Your First Program: "Hello, World!"
Right-click anywhere on a webpage. Select "Inspect" or "Inspect Element". A new panel will open. Click on the "Console" tab.
Open the console in your browser. Type the following line and press Enter:
console.log("Hello, World!");
The Ingredients of Code: Data Types
Patient's Name: This is text (e.g., "John Doe").Date of Birth: This involves numbers and symbols (e.g., "10/25/1990").Marital Status: This is a choice between options, like "Single" or "Married". We could represent this as a simpleyes orno decision (e.g.,IsMarried? true).
Numbers: Used for mathematical calculations. They can be whole numbers (integers) or have decimals.Examples: 100, -55, 3.14
Strings: Used for text. A string is any sequence of characters wrapped in quotes (" or ').Examples: "hello", 'JavaScript is fun', "123" (This is text, not a number, because of the quotes!)
Booleans: Used for logical operations. A boolean can only have one of two values:true orfalse . They are perfect for answering yes/no questions.Examples: true, false
Storing Information: Meet Variables!
Without a variable: To get the total, you'd have to pick up every single item again and add up their prices: $1500 + $800 + $300 + $800. This is inefficient, especially for a big cart!With a variable: You keep a running total. You start at $0. You add the camera ($1500), and your total is now $1500. You add the hard drive ($800), and your total becomes $2300. You only need to remember one thing: the current total.
A variable is a named container used to store, change, and access information. Think of it as a labeled box where you can put data.
Declaration: Creating the empty, labeled box.Assignment: Putting something inside the box.Reassignment: Replacing the contents of the box with something new.
The Three Keywords: var, let, and const
1. var (The Old Way)
2. let (The Modern, Flexible Way)
3. const (The Constant, Unchanging Way)
Quick Summary: var vs. let vs. const
The Rules of the Name Game: Naming Variables
Variable names can start with a letter, an underscore (, or a dollar sign (. After the first character, you can use letters, numbers, underscores, or dollar signs.
You cannot start a variable name with a number. (let 1stPlace = "Alex"; is invalid). You cannot use JavaScript's reserved keywords (like let, const, var, function, etc.). (let const = 10; is invalid).
let firstName = "Jane"; (Good) const itemsInCart = 5; (Good) let firstname = "Jane"; (Okay, but less readable) let items_in_cart = 5; (Also okay, called snake_case, but camelCase is more common in JS)
Knowledge Check
Conclusion & What's Next
JavaScript makes web pages interactive. console.log() is your best friend for seeing what's happening in your code.Data comes in different types, like Numbers, Strings, and Booleans. Variables are labeled boxes for storing data, created with let (for things that change) andconst (for things that don't).
Comments
Post a Comment