Variables in JavaScript. #JS-Shorts [9]

Variables are used to store data temporarily in a computer's memory. So, computer store data somewhere and give that a memory location and name...

Variables in JavaScript. #JS-Shorts [9]

Variables are used to store data temporarily in a computer's memory.

  • So, computer store data somewhere and give that a memory location and name.
  • And with that name we can read the data at the given location.
  • See the example below↓  myNumber is the name of variable stored in a computer memory having value 23 at a computer assigned address.

Variable Declaration in Code.

Before ES6 we user var keyword to declare a variable. But, from ES6 best pratice is to use let keyword to declare a variable.

  1. Declare a variable begin with let keyword.
  2. then variable name or an identifier.
  3. terminate this declaration with semicolon ;
  4. Example see below
let name;

Now, let's log the value of variable name in the console .

console.log(name);

By default the value of variable in JavaScript is undefined.

We can see (in image below↓) the value of variable name in the console at line no. 6 comes undefined

We can (optionally) initialize the variable with string (which is a sequence of characters).

We can use single quote 'Akhil' OR double quote "Akhil" | but, it's most comman and best practice to use 'single quote' to declare variables in JavaScript.

Example:

let name = 'Akhil';
console.log(name);
Declared variable name and assigned 'Akhil' as a value of that variable.

Rules for Naming these Variables.

  • They can not be the reserved keyword.  In JavaScript let var const if else for function these are reserved keyword.
  • Variables name should be meaningful. The should not be a or b or x. The variable name should give us a clue what is the purpose of the variables. It's best practice to always use meaningful and descriptive names.
  • The variable name can-not start with a number. Example of incorrect variable name declaration 1name 4colors 7days ← Incorrect!
  • Variable name can-not contain a space or hyphen. Example of incorrect variable  first name or first-name ← Incorrect!
  • Correct way to declare variable with multiple words are firstName.
let firstName = 'Akhil';

Note: In JavaScript we use CamelNotation to name a variable. CamelNotation: First letter of the first word should be lowercase and first word of every word after should be uppercase this is what we called camelNotation. Example:  firstName

  • Variable names are case-sensitive, see ↓ example: these two variables are different.
let firstName;
let FirstName;
  • There are two ways to declare multiple variables.

We can declare multiple variables in one line and separate them using comma ,

let firstName, lastName; ← Valid (with default value undefined)

let firstName = 'Akhil', lastName;← Valid (one initialized and one undefined, we can initialize both this way)

But, it's best practice to declare evey variable in a single line.

let firstName; ← Best practice (declare one variable in a single line)

let firstName = 'Akhil'← Best practice (declare one variable in a single line)


In this article we have covered:

How to declare variables and rules for naming these variables.


Next

Next will be soon!