JavaScript for Beginners: Chapter 1 – Variables

This first chapter is all about variables, the way to declare and change them as well as some of the naming rules and different types of variables.

What are variables?

A variable is named storage for data. Each variable must have a unique n…


This content originally appeared on DEV Community and was authored by Danish Saleem

This first chapter is all about variables, the way to declare and change them as well as some of the naming rules and different types of variables.

What are variables?

A variable is named storage for data. Each variable must have a unique name which is called an identifier. Variables can be hard-coded to hold predefined values of dynamically generated.

Declaring variables

You can easily declare a variable using the let keyword followed by the name of the variable.

let userName;

userName = "Dev";

You have the option to declare a variable and assign a value later or do both at once.

let userName = "Dev";
let loggedIn = true;

Changing value

You can easily change the value of a variable by easily assigning a new one.

let testVariable = "Hello";

testVariable = "World";

JavaScript dynamically types so you can even assign a value of a different type (although that is not advised)

let testVariable = "Hello";

testVariable = 10;

testVariable = false;

Naming rules

JavaScript variable names can only contain letters, numbers or the symbols $ and _

However, the first character can't be a number and you can't use reserved keywords (such as let or return) for variable names.

// Valid variable names
let test;
let test1;
let variable_name;
let user_1;
let userName;

// Invalid variable names
let 1test;
let variable-test;
let return;

Constants

If the value of your variable is constant and won't be re-assigned you can use the const keyword instead of let

const welcomeMessage = "Hello World!";

These are useful to create a distinction between constant and changeable variables.

const userName = "Dev";
let isLoggedIn = true;

Uppercase Constants

It has also become common practice to use constant aliases for known values that are used throughout a site or application. These are written with uppercase letters and underscores.

const PRIMARY_BRAND_COLOUR = "#1c3aa4";
const CONTACT_EMAIL = "dev@mail.com";
const COMPANY_REG_NUMBER = "9876543210";

There is technically no difference between them as far as JavaScript is concerned, it is just a style choice to differentiate them.

What about var?

You can still declare variables using var, however, the practice is now considered outdated.

// Old standard
var message = "Hello World!";

// New standard
let message = "Hello World!";

Variable declared with var aren't scoped and have various other flaws, so unless you need to support a really old browser it's best to use let and const variables.

Summary

  • Use let to declare variables with changeable values.
  • Use const to declare constant, non-changeable values.
  • Follow the naming rules and name your variables well.
  • User uppercase constants for hard-coded global constant values.
  • Don't use var to declare variables unless you require legacy browser support.

Let's connect 💜

You can follow me on Twitter, Instagram & GitHub

If you like this post. Kindly support me by Buying Me a Coffee

Buy Me a Coffee


This content originally appeared on DEV Community and was authored by Danish Saleem


Print Share Comment Cite Upload Translate Updates
APA

Danish Saleem | Sciencx (2021-12-01T12:43:11+00:00) JavaScript for Beginners: Chapter 1 – Variables. Retrieved from https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/

MLA
" » JavaScript for Beginners: Chapter 1 – Variables." Danish Saleem | Sciencx - Wednesday December 1, 2021, https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/
HARVARD
Danish Saleem | Sciencx Wednesday December 1, 2021 » JavaScript for Beginners: Chapter 1 – Variables., viewed ,<https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/>
VANCOUVER
Danish Saleem | Sciencx - » JavaScript for Beginners: Chapter 1 – Variables. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/
CHICAGO
" » JavaScript for Beginners: Chapter 1 – Variables." Danish Saleem | Sciencx - Accessed . https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/
IEEE
" » JavaScript for Beginners: Chapter 1 – Variables." Danish Saleem | Sciencx [Online]. Available: https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/. [Accessed: ]
rf:citation
» JavaScript for Beginners: Chapter 1 – Variables | Danish Saleem | Sciencx | https://www.scien.cx/2021/12/01/javascript-for-beginners-chapter-1-variables/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.