JavaScript Variables Explained

➡️ 1. What is a Variable in JavaScript?

In web development, a variable is a foundational container used to store data values that your website can use, change, or update dynamically. Think of a variable as a labeled storage box. If you want your website to remember a visitor's name, their current shopping cart score, or whether they are logged in, you pack that data into a memory box and slap a clean label on it.

While Python lets you create variables by just typing their name, JavaScript requires you to explicitly declare that you are creating a new variable using special keyword keywords. JavaScript is also a **dynamically typed language**, meaning a single box can hold numbers, text strings, or logical states without you needing to pre-define the data type layout.

➡️ 2. Declaring Variables: let, const, and var

Modern JavaScript provides three unique keywords to create storage containers. Choosing the right keyword dictates how your data can behave over time:

// Declaring changeable and constant data streams
let digitalScore = 250;
const frameworkName = "JavaScript";

// Outputting the information directly to the browser console
console.log(digitalScore);
console.log(frameworkName);

➡️ 3. Line-by-Line Execution Breakdown

Let's look at exactly what happens inside your browser's V8 engine layout when the script above runs:

➡️ 4. Overwriting Variable Values

If you declared a variable using the let keyword, you can easily alter its interior contents as your application logic runs. However, notice that when you overwrite a variable, you **do not** re-type the keyword!

let userTokens = 10;
console.log(userTokens); // Outputs: 10

// Updating the value (No 'let' keyword needed here!)
userTokens = 35;
console.log(userTokens); // Outputs: 35

// Modifying the variable relative to itself
userTokens = userTokens + 5;
console.log(userTokens); // Outputs: 40

➡️ 5. Mistakes that Crash JavaScript Applications

Web browsers are strict when compiling script logic. Watch out for these common beginner errors to keep your code running cleanly:

➡️ 6. Hands-On Frontend Practice

Let's write some frontend script logic! Open your code editor and attempt this challenge: Create a mutable variable named websiteViews using the correct keyword and assign it an initial number. On the next line, update the variable by adding 100 to its current value. Finally, create a constant variable named developerName, set it to your name as a string, and pass both variables to console.log() statements.

Arrays