Programming/JavaScript

JavaScript variable and constant declarations, data types

Isaac S. Lee 2023. 6. 15. 15:51

Variables and Constants Declaration

In JavaScript, variables and constants are identifiers used to store values.
Variables are used for data that can change, while constants are used for data that cannot be changed after assignment.

 

Variable Declaration with let

Variable declaration is done using the let keyword. When a variable is declared with let, it has block scope.
Block scope means that the variable is only valid within the curly braces ({}) block where it is declared. Therefore, a variable declared with let cannot be accessed outside of the block.

let age = 23;

Here, a variable named "age" is declared and assigned a value of 23.

 

let age = 23;
let age = 24; // Error: Variable 'age' has already been declared

The let keyword does not allow redeclaration of variables. If you try to declare a variable with the same name within the same scope, an error will occur.

 

 

Constant Declaration with const

Constants are declared using the const keyword.

const pi = 3.14;

Here, a constant named "pi" is declared and assigned a value of 3.14.

Caution When Using var

var name = "Isaac";

The var keyword was used in older versions of JavaScript for variable declaration. However, since ES6, it is recommended to use let and const for variable declaration.

 

Variable Scope

Variables declared with var have function scope. This means that the scope of the variable is limited to the function where it is declared. Var variables declared inside a function cannot be accessed outside of the function.

 

Hoisting

Variables declared with var are hoisted. Hoisting is a behavior where variable declarations are moved to the top of their scope. This means that a variable can be used before it is declared. However, the value of the variable will be undefined until it is assigned.

 

Duplicate Declaration Allowed

Variables with the same name can be declared multiple times using var. In such cases, the previously declared variable will be ignored, and the new variable will be assigned.


No Block Scope Support

Variables declared with var do not support block scope. Variables declared within if statements or for loops, for example, have the same scope as the surrounding function scope. This can lead to variable leakage and potential issues.

 

Data Types

Number Type

The number type represents integers and decimal numbers.

let number = 42;

Here, a variable named "number" is declared and assigned a value of 42.

 

Arithmetic Operations

Various arithmetic operations can be performed on number types, including addition, subtraction, multiplication, and division.

let x = 10;
let y = 5;
let sum = x + y;
let difference = x - y;
let product = x * y;
let quotient = x / y;

 

Other Number Operators

For number types, there are additional operators available, such as the remainder operator (%), increment operator (++), and decrement operator (--).

let remainder = x % y; // remainder operator
let increment = ++x; // increment operator
let decrement = --y; // decrement operator

 

String Type

The string type represents text data.

let name = "Isaac";

 

Boolean Type

The boolean type represents true and false.

let isTrue = true;

 

Array Type

배열 타입은 여러 개의 값들을 순서대로 저장한다.

let numbers = [1, 2, 3, 4, 5];

 

Object Type

The object type represents a collection of properties and values.

let person = {
  name: "Isaac",
  age: 23,
};

 

Data Type Conversion

To Number Conversion

You can use the parseInt() or parseFloat() functions to convert a string to an integer or floating-point number, respectively. These functions parse the string and convert it to a number.

let numberAsString = "100";
let parsedNumber = parseInt(numberAsString);
console.log("Parsed Number:", parsedNumber); // Output: 100

 

To String Conversion

To convert a number or other data types to a string, you can use the toString() method or concatenate the value with an empty string ("").

let number = 100;
let numberAsString = number.toString();
console.log("Number as String:", numberAsString); // Output: "100"

let booleanValue = true;
let booleanAsString = "" + booleanValue;
console.log("Boolean as String:", booleanAsString); // Output: "true"

 

To Boolean Conversion

Explicit conversion to a boolean value can be done using the Boolean() function. The general rule is that falsy values (e.g., 0, empty string, null, undefined, NaN) are converted to false, while other values are converted to true.

let number = 100;
let booleanValue = Boolean(number);
console.log("Number as Boolean:", booleanValue); // Output: true

let emptyString = "";
let emptyStringAsBoolean = Boolean(emptyString);
console.log("Empty String as Boolean:", emptyStringAsBoolean); // Output: false