Hello Dev Family ๐... I hope you are doing good and safe. Today we will talk about JavaScript.JavaScript is versatile and beginner-friendly.JavaScript itself is relatively compact, yet very flexible. Developers have written a variety of tools on top of the core JavaScript language, unlocking a vast amount of functionality with minimum effort.
However, getting comfortable with JavaScript is more challenging than getting comfortable with HTML and CSS. You may have to start small, and progress gradually. To begin, let's have look at this article. This is the first article in my new JavaScript for Beginners series.
This first article is all about variables, the way to declare and change them, and 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 or dynamically generated.
Declaring variables
You can easily declare a variable using the let keyword followed by the name of the variable.
let firstName;
firstName = "Harshada";
You have the option to declare a variable and assign a value later or do both at once.
let firstName = " Harshada";
let isActiveUser = true;
Changing value
You can easily change the value of a variable by easily assigning a new one.
let testVariable = "Hello";
testVariable = "World";
JS is dynamically types do you can even assign a value of a different type (although that is not advised)
let testVariable = "Hello";
testVariable = 10;
testVariable = false
Naming rules
JS variable names can only contain letters,number 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 test1;
let variable_name;
let user_1
// 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 const instead of let.
const welcomeMessage = "Hello Dev Fam";
These are useful to create a distinction between constant and changeable variables.
const firstName = "Harshada";
let isActiveUser = 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_APP_COLOR = "#dcdcdc";
const CONTACT_EMAIL= "dev@getnada.com";
const COMPANY_REG_NUMBER = "G5846985";
There is technically no difference between them as far as JS 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 dev fam";
// New standard
let message = "Hello dev fam";
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 value.Follow the naming rules and name your variable well.
Use uppercase constants for hard-coded global constant values.
Don't use var to declare variables unless you require legacy browser support.
Thank you for reading, I hope you find this useful. If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback! and stick around for new articles which will be published weekly!