JavaScript - The Complete Guide 2022

Section 16: Numbers & Strings - Deep Dive - Behind the Scenes & Beyond the Basics

olivia_yj 2022. 11. 2. 07:02

The goals

๐Ÿ’ช๐ŸปMore about Numbers

โœŒ๐ŸปWorking with the Number & Math Objects

๐Ÿ‘๐ŸปMore about Strings & Template Literals

 

Javascript internally works with the binary system as we learned

Therefore Javascript converts this number to the binary system for doing the calculation and then basically converts it back to give us an output that makes sense to us humans because we rather work with the decimal system than with the binary system.

 

 

0.2 is equal to 1/5 in decimal system

0.4 is equal to 2/5 in decimal system

but in binary system, it is totally different number

So this is how Javascript performs the calculation and tries to show us the number we can understand

๊ทธ๋ž˜์„œ ์—ฐ์‚ฐ์‹œ์— ์ € ์˜ค์ฐจ๋•Œ๋ฌธ์— ์™„๋ฒฝํžˆ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์•„์„œ ๋“ฑ์‹์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

1. ์ •ํ™•ํžˆ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด ์ •์ˆ˜๋กœ ํ•œ๋‹ค (ํŠนํžˆ๋‚˜ ์ •ํ™•ํ•ด์•ผ ํ•˜๋Š” ๋ˆ์ด๋‚˜ ์ฝ”์ธ์˜ ๊ฒฝ์šฐ)

e.g. 5.1๋‹ฌ๋Ÿฌ -> 5100์„ผํŠธ

 

2. ๋ฐ˜์˜ฌ๋ฆผ ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•œ๋‹ค

e.g. precision = Math.pow(10, precision)

       Math.ceil(num * precision) / precision

 

3. double ์ž๋ฃŒํ˜•

์• ์ดˆ์— ์ˆซ์ž๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•  ๋•Œ ์ˆซ์ž 1๊ฐœ๋‹น 64์นธ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์ •ํ•จ 

-> ๋‹จ์ : ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰ 2๋ฐฐ ํ•„์š”

 

์ด๋Ÿฐ ์ ์„ ์ž˜ ์•Œ๊ณ  ์žˆ์œผ๋ฉด overflow, Not a Number, infinityํ˜„์ƒ ์ดํ•ด์— ๋„์›€์ด ๋จ!

 

BigInt()

BigInt๋Š” ์ˆซ์ž๊ฐ’์„ ์•ˆ์ •์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€์น˜๋ณด๋‹ค ํฐ ์ •์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ์›์‹œ๊ฐ’์ด๋‹ค. BigInt๊ฐ’์€ ์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด์˜ ๋’ค์— n์„ ๋ถ™์ด๊ฑฐ๋‚˜ (10n) BigIntํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

BigInt is a primitive wrapper object used to represent and manipulate primitive bigint values โ€” which are too large to be represented by the number primitive.

BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() function (without the new operator) and giving it an integer value or string value.

 

Math.random()

Math.floor()

 

 

Tagged Template

It can be useful when we have a scenario where we conveniently want to create some output, could be a string but could be something totally different as well, based on some string input.

 

What is Tagged Template Literals?

Tagged template literals were introduced in the ES6 version of JavaScript. Iโ€™m safely assuming that most of you are hopefully aware of template strings, where you can interpolate dynamic content inside a string.

let greetings = "Hi";
let name = "Techsith";
let age = 35;

let statement = `${greetings} my name is ${name} and I am ${age}`;

The code sample above will render โ€œHi my name is Techsith and I am 35โ€ as expected. In this template string, โ€œ my name is โ€œ and โ€œ and I am โ€ are static content and greetings, name, and age are dynamic content. There are use cases where you want to take the user provided template string and perform some transformation to the dynamic content based on some logic. Tagged template literals let you extract all the dynamic values, which are called tags, and segregate them from the static content. This gives us the ability to transform these tags and interpolate it with the static content to derive a new string.

let greetings = "Hi";
let name = "Techsith";
let age = 35;

function transform(static, ...tags) {
  console.log(static); // ["", " my name is ", " and I am ", ""]
  console.log(tags); //["Hi", "Techsith", 35]
}

transform`${greetings} my name is ${name} and I am ${age}`;

In the above example, weโ€™ve created a function called transform. It takes two arguments: static array that holds all the static content and tags array that holds all the tags (dynamic content). When you run this function, it prints the following values, which are segregated based on content.

static => [โ€œโ€, โ€œ my name is โ€œ, โ€œ and I am โ€œ, โ€œโ€]
tags => [โ€œHiโ€, โ€œTechsithโ€, 35]

By interpolating static array with tags array, you can reconstruct the original string. When you dissect the output, youโ€™ll get empty strings if the tags are placed at the beginning or at the end of the string, respectively.

Tagged Templates Interpolation

Letโ€™s transform the name tag value Techsith to Hemil and reconstruct the string.

 

let greetings = "Hi";
let name = "Techsith";
let age = 35;

function transform(static, ...tags) {
  let index = tags.findIndex((el) => el === "Techsith");
  tags[index] = "Hemil";

  let str = static[0];
  for (i = 0; i < tags.length; i++) {
    str += tags[i] + static[i + 1];
  }
  return str;
}

let newString = transform`${greetings} my name is ${name} and I am ${age}`;

console.log(newString); //"Hi my name is Hemil and I am 35"

 

RegEx

์ •๊ทœํ‘œํ˜„์‹ regular expression์€ ์ผ์ •ํ•œ ํŒจํ„ด์„ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์˜ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹ ์–ธ์–ด (formal language)๋‹ค. ์ •๊ทœํ‘œํ˜„์‹์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ณ ์œ  ๋ฌธ๋ฒ•์ด ์•„๋‹ˆ๋ฉฐ, ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ์ฝ”๋“œ ์—๋””ํ„ฐ์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํŽ„ perl์˜ ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•์„ ES3๋ถ€ํ„ฐ ๋„์ž…ํ–ˆ๋‹ค.

 

์ •๊ทœํ‘œํ˜„์‹์€ ๋ฌธ์ž์—ด์„ ๋Œ€์ƒ์œผ๋กœ ํŒจํ„ด ๋งค์นญ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ํŒจํ„ด ๋งค์นญ ๊ธฐ๋Šฅ์ด๋ž€ ํŠน์ • ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์ถ”์ถœ ๋˜๋Š” ์น˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๋งํ•œ๋‹ค.

 

 

 

 

 


Sources

https://www.youtube.com/watch?v=-GsrYvZoAdA&ab_channel=%EC%BD%94%EB%94%A9%EC%95%A0%ED%94%8C 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

 

BigInt - JavaScript | MDN

BigInt is a primitive wrapper object used to represent and manipulate primitive bigint values โ€” which are too large to be represented by the number primitive.

developer.mozilla.org

https://patelhemil.medium.com/magic-of-tagged-templates-literals-in-javascript-e0e2379b1ffc

 

Magic of Tagged Templates Literals in JavaScript?

Once in a while you come across a JavaScript syntax that looks strange in nature. However, thatโ€™s not quite true, letโ€™s phrase itโ€ฆ

patelhemil.medium.com