0% found this document useful (0 votes)
3 views

Chapter 2 - Variables for strings

A variable in JavaScript is a storage box for information that allows you to store and reuse values like text and numbers. You declare a variable using 'var' and can change its value later without redeclaring it. Meaningful variable names enhance code readability, and text strings require quotation marks while variables do not.

Uploaded by

Manahil Aleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 2 - Variables for strings

A variable in JavaScript is a storage box for information that allows you to store and reuse values like text and numbers. You declare a variable using 'var' and can change its value later without redeclaring it. Meaningful variable names enhance code readability, and text strings require quotation marks while variables do not.

Uploaded by

Manahil Aleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

What is a Variable?

A variable is like a storage box for information in JavaScript. You


use it to store values (like text, numbers, etc.) and reuse them later
in your code.

Example:
var name = "Mark";

 Here, var is a keyword that tells JavaScript, "I'm creating a new


variable."
 name is the variable's name (you can choose any name).
 "Mark" is the value stored in the variable.
 The equal sign = assigns the value to the variable.

So, now name means "Mark" in JavaScript. Whenever you use name,
JavaScript understands that it refers to "Mark".

Why Use Variables?

Instead of writing "Mark" every time, you can just write name. This
makes coding easier and more flexible.

Example:
alert(name); // This will show a pop-up with "Mark"

Even though we wrote name, JavaScript knows it means "Mark", so it


displays "Mark" in the alert.
Variables Can Change

A variable does not have to keep the same value forever. You
can change it later.

Example:
var name = "Mark";
name = "Ace";
alert(name); // Now it shows "Ace" instead of "Mark"

 At first, name was "Mark".


 Then, we changed it to "Ace".
 JavaScript remembers only the latest value ("Ace").

👉 Important: We didn’t write var again when updating name. This is


because we already declared it earlier.

Creating a Variable Without a Value

You can create a variable first and assign a value later.

Example:
var nationality; // The variable is created but empty
nationality = "U.S."; // Now it has a value

This is useful if you don’t know the value at first but will add it later.

JavaScript Doesn’t Care About Variable Names

You can name a variable anything (as long as it follows the rules).
var floogle = "Mark";
var x = "Mark";
var lessonAuthor = "Mark";
var guyWhoKeepsSayingHisOwnName = "Mark";

👉 JavaScript does not care what you name your variables, but
you should choose meaningful names so your code is easy to
understand.

Good variable names:


✅ firstName, age, city

Bad variable names:


❌ x, y, thing, data

Text Strings vs. Variables

 A text string (words inside " " or ' ') always needs quotation marks.
 A variable never uses quotation marks.

Example:
var nickname = "Bub"; // Correct (nickname is a variable, "Bub" is a string)
alert(nickname); // Shows Bub

If you write alert("nickname");, it will display "nickname" instead of "Bub".

Using Variables in Alerts

Instead of writing this:


alert("Thanks for your input!");

You can store the message in a variable:


var thanx = "Thanks for your input!";
alert(thanx);

👉 Both versions show the same message!


This is useful when you want to change the message later without
editing multiple places in your code.

Final Summary

 A variable stores a value (like a name, number, or message).


 Use var to declare a variable, and = to assign a value.
 Variables can change (you don’t need to use var again when changing
them).
 Variable names should be meaningful for easy understanding.
 Text strings need quotes (" "), but variables don’t.
 Using variables saves time and makes code cleaner.

You might also like