[2022.10.08(Sat)] JavaScript #2 - Variables and Data Types

Jun's Coding Journey·2022년 10월 8일
0

[Learn] JavaScript

목록 보기
3/9

Values

Values are pieces of data that are stored and used for certain programming purposes. In programming terms, a value is the smallest unit of information in a code.

Variables

One of the most useful functions of a value is that it can be stored into variables. This way, it can be re-used anytime whenever needed. As such, whenever we need to find a certain value, all we would need to do is look for the right variable that stores the value.

let firstName = 'John';
let age = 8;

For an easier approach, we can think of storing things into a box. In this case, the box that is able to store things would be the variable and the things that would go into the box would be the values. Whenever we need to look for the values we want, we would simply find the name of the box.

But how would we be able to see the results? We would simply use the console.log method to visually make it appear within the chrome developer tool.

let firstName = 'John';
console.log(firstName); // John

Another useful function of variables is that we can change the value inside the variable. Say that we have a variable of 'firstName' with a value of 'John' inside and a bunch of console.log lines. If we decide to change the value inside the variable, what would be the appropriate approach?

let firstName = 'John';
console.log(firstName); // John
console.log(firstName); // John
console.log(firstName); // John

Instead of trying to literally change each individual line of code one-by-one, we could simply just change the value itself. This way, all the other lines would change automatically.

let firstName = 'David';
console.log(firstName); // David
console.log(firstName); // David
console.log(firstName); // David

Although simple, revisiting these topics again reinforces very crucial basic knowledge that helps me become a better developer in the future. As such I will continue to strive through this journey 🙂

Data Types

Variables can store various types of data in their container. Before going into it any further, it is important to first address the categories within types of data. A single value can either be categorized as an object or a primitive value. For this post, I will only talk about primitive values and explain object values in a future post (as it is easier to follow-through that way 🙂).

7 Primitive Data Types

1) Number: Any number value that is shown as either a decimal or an integer.

let age = 23;
let decimal = 1.01;

2) String: Sequence of characters used for text.

let firstName = 'John';
let lastName = 'Smith';

3) Boolean: Logical type that can only be true or false which is used for taking decisions.

let isTrue = true;
let isFalse = false;

4) Undefined: a value taken by a variable that is not yet defined ('empty value').

let container;

5) Null: similar to undefined, null also means 'empty value', but done intentionally.

let container = null;

6) Symbol (ES2015): a value that is unique and cannot be changed.

7) BigInt (ES2020): larger integers than the Number type can hold.

Typeof

The typeof operator is used to determine the type of a particular data in string format. The typeof operator is useful because it is an easy way to check the type of a variable in a code. This is important because JavaScript is a is a dynamically typed language (more on this below). This means that you aren’t required to assign types to variables when you create them.

let word = 'hello'
typeof word; // 'string'
let num = '1';
typeof num; // 'number'
let isTrue = true;
typeof isTrue; // 'boolean'

More on Data Types

Although there are many types of data that can be used in JavaScript, developers only mostly use number, string, and boolean (sometimes undefined and null). However, I feel it is also useful to know the rest as well once I become more fluent in JavaScript.

One useful function of JavaScript is that it has dynamic typing. This means that I do not have to manually define the data type of the value stored in a variable. Instead, data types are determined automatically.

profile
Greatness From Small Beginnings

0개의 댓글