DEV Community

Cover image for Juggling JavaScript: A Dive Into Variables.
Tshegofatso Letlape
Tshegofatso Letlape

Posted on

Juggling JavaScript: A Dive Into Variables.

In the world of programming variables serve as the foundational building blocks for storing your data and manipulating it. Below I share what I have learned about this concept.

Exhibit A: Declaring Variables
Variables are like boxes, ready to hold all sorts of goodies. JavaScript allows us to declare variables using the let, const, or even var keyword. The difference? let allows reassignment, while const grants us the power to keep constants constant (unchangeable).

let firstName = "Tshegofatso"; // Declared variable called firstName and assigned Tshegofatso as value.
const pi = "3.14159"; // This will never change I'm afraid lol
Enter fullscreen mode Exit fullscreen mode

Exhibit B: Variable Naming
In JavaScript, naming variables is important. Variables should be named in a way that allows us to easily understand what’s inside them. Variable names can include letters, digits, underscores, and a dollar signs. But they can't start with a digit and cannotContainSpaces. When a name you have for a variable contains multiple words, we use camelCase (words go one after the other with no space, each word except the first one starts with a Capital letter) this helps you to have readable and neat variable names.

let myFavoriteNumber  = 14 ;
const myHomeCity = "Johannesburg";

Enter fullscreen mode Exit fullscreen mode

Exhibit C: Assigning Values to Variables
After creating the variable we can give it a value. The equal sign = is used to assign a value to our variable. Variables can hold any type of data: numbers, strings, booleans, objects, arrays, and more. Text values are always enclosed in quotes.

let name = "Tshego"; 
let height = 163;
const greeting = "Hello, World";
Enter fullscreen mode Exit fullscreen mode

Exhibit D: Constants in Detail
There will be times when you need to tell the program that the variable can't change it's value throughout the program, this is where constants const comes in. Constants must have a value when declared and they cannot change their value.

const colour = 'purple';
console.log(colour); // Output to color will always be purple throughout the program
Enter fullscreen mode Exit fullscreen mode

... And I think that wraps up my summary about variables.

Top comments (3)

Collapse
 
codejem profile image
Jemima M

Brilliant!

Collapse
 
cedriclapi profile image
CedricLapi

i love it!!!

Collapse
 
tshegoletlape profile image
Tshegofatso Letlape

Thank you Cedric.