[JavaScript] Variables

Cadet_42·2021년 7월 19일
0

JavaScript

목록 보기
2/8
post-thumbnail

What is a variable?

Let's start with an example from real life.
You decided to pack all your stuff into different boxes and place them into some storage. You put labels on each box so you can easier find it later, and obviously, the name is descrptive, so you know what is in that box.

Analogy:

  • Storage is the memory,
  • the box is the variable,
  • the box lbel is the variable's name,
  • the box content is the variable's value.

The main purpose of variable is to store some information in them. These variables are labeled so whenever we need the information inside we can easily access them. A very common way of saying - access to variable - is reference a variable by its name.

Variable declaration and initialization

Variable declaration

  • Creating a variable and giving it a name is the first step of variable declaration.
  • Variable declartion is the act of just creating and labeling an empty box that will later be filled with some information (its value).

To declare of define a variable, we can use the keyword let or const and var (in oler version of js).

Variable declaration with let

  • Variable declaration : We put the name/label on the boxes. The value/information will be added to it later.
let age; // <== this is a variable declaration 
let name, email, address; // <== even able to decalre multiple variables in the same line while separating them with a comma. 

Variable initialization with let

  • After we declared a variable, the next step is to store some value in it. This process is called Variable initialization

  • To put some data into the variablem we use the assignment operator = .

let name; // <== this is a variable declaration
name = 'Hello'; // <== this is a variable initialization

Variable declaration with const

const is used when declaring a variable in which the value will be constant

  • this means that the value we assigned to a variable won't ever change (in the case of strings, numbers, booleans).
const name = 'Lina';
name = 'Maria';
 
// console:
// unknown: "name" is read-only

To summarize, we can use let when we expect that the variable will change its value. However, use const when you’re sure that the type of the variable will remain unchanged.

Naming a variable

A name is simply a reference for a variable.

let companyName = 'Apple';
let age = 3;

Variable value

  • A value can be any of the JavaScript value types: String, number, array, object, etc.

Naming Rules

  1. Names can contain letters (uppercase and lowercase), numbers, and the symbols _ and $.
  2. The first character of the name can’t be a number.
  3. When creating a variable with more than one word, we use the "camelCase" style.
  4. Although we said you could use any English or non-English word, still there’s a bit constraint: there are some reserved keywords that can’t be used, for example: let, class, return, and function are reserved.
let let = 'hello'; // <== error, you can't name variable "let"

Changing Values

The value of a variable declared with let can be changed whenever you want.
To change a value, we juste need to reference the variable by its name and assign a new value:

let favoriteFood;
favoriteFood = "Steak";
console.log(favoriteFood); // <== "Steak"

favoriteFood = "Burger";
console.log(favoriteFood); // <== "Burger"

Basic(primitive) data types in JavaScript:

  • string,
  • number,
  • boolean (True / False)

Non-primitive data types:

  • object,
  • array.

To see what is the data type of any variable, you can use the JavaScript operator, typeof

let favoriteFood;
 
favoriteFood = 'Steak';
console.log('Value: ', favoriteFood, ' Type: ', typeof favoriteFood);
 
favoriteFood = 20;
console.log('Value: ', favoriteFood, ' Type: ', typeof favoriteFood);
 
// console:
// Value:  Steak  Type:  string
// Value:  20     Type:  number

💡💡 REMEMBER 💡💡

  • You can only reassign the variable and changes its value and type if you use let when declaring a variable.
  • To help yourself and others who work or will work on the same code, try to use cosnt whenever there is a chance to do so.

Important naming rules

Naming variables is hard, and it is, in fact, one of the hardest in programming. You should try yourbest to name variables in a meaningful way.

Variable names like i, j, k, (some exceptions to these), something, object, or variable do not give any indication as to what the value of that variable is. You are more than likely not going to be the only person reading your code.

Wrong naming rules 😟

let x, y, z, zz;

x = 1;
y = 2;
z = (x + y) / 2;
zz = "The average of two numbers is " + z;
console.log(zz); 

Good naming rules 😁

var firstNum, secondNum, average, message;

firstNum = 1;
secondNum = 2;
average = (firstNum + secondNum) / 2;
message = "The average of the two numbers is " + average;
console.log(message);

Lessons learned 🎉

  • what a variable is,
  • how to declare variables,
  • how to name variables correctly.
profile
안녕하세요! 개발공부를 하고 있습니다. 감사히 배우겠습니다. ;)

0개의 댓글