import{_ as t,o as l,c as o,a as s,b as e,e as i,d as a,r as p}from"./app.df5d2fa1.js";const T=JSON.parse('{"title":"1-1: Values","description":"Chapter 1-1 notes on values","frontmatter":{"title":"1-1: Values","description":"Chapter 1-1 notes on values"},"headers":[{"level":2,"title":"1-1-1: Numbers","slug":"_1-1-1-numbers","link":"#_1-1-1-numbers","children":[]},{"level":2,"title":"1-1-2: Arithmetic","slug":"_1-1-2-arithmetic","link":"#_1-1-2-arithmetic","children":[]},{"level":2,"title":"1-1-3: Strings","slug":"_1-1-3-strings","link":"#_1-1-3-strings","children":[{"level":3,"title":"1-1-3-FR: Further Reading","slug":"_1-1-3-fr-further-reading","link":"#_1-1-3-fr-further-reading","children":[]}]}],"relativePath":"javascript/notes/1/1-1.md","lastUpdated":1677731760000}'),r={name:"javascript/notes/1/1-1.md"},c=s("h1",{id:"_1-1-values",tabindex:"-1"},[e("1-1: Values "),s("a",{class:"header-anchor",href:"#_1-1-values","aria-hidden":"true"},"#")],-1),d={id:"_1-1-1-numbers",tabindex:"-1"},h=s("a",{class:"header-anchor",href:"#_1-1-1-numbers","aria-hidden":"true"},"#",-1),u=a(`
Values of the number type are numeric values, normally written as follows,
js
144
144
1
Put this into a program, it will cause the number 144 to come into existence inside the computer, with the following bash script, 144 might looks like this in bits,
With the following bash script, 144 will be converted into binary values in integer form,
sh
ip1=144# defines a variable named ip1, with the value of 10echo"obase=2;$ip1"|bc# convert the value to binary via bc
ip1=144# defines a variable named ip1, with the value of 10echo"obase=2;$ip1" | bc # convert the value to binary via bc
1 2
The variable ip1 is assigned the value 144.
The echo command prints out the string "obase=2;144", where "obase=2;" is an argument for bc that tells it to output the result in base 2 (binary), and "10" is the decimal number we want to convert.
The output of the echo command is piped (using the | character) to bc, which takes the input "obase=2;144" and interprets it as a command to convert the number 10 to binary.
Finally, the binary equivalent of 144, which is "10010000", is printed to the terminal.
TIP
But the standard describes JavaScript numbers as 64-bit floating-point values, which indicates fractions and exponents are available.
`,4),m=s("p",null,[e("Not all whole numbers "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("mn",null,"1"),s("msup",null,[s("mn",null,"0"),s("mn",null,"19")])]),s("annotation",{encoding:"application/x-tex"},"10^{19}")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},"1"),s("span",{class:"mord"},[s("span",{class:"mord"},"0"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},[s("span",{class:"mord mtight"},"19")])])])])])])])])])])]),e(" fit in a JavaScript number. There are also negative numbers, so one of the bits has to be used to store the sign of the number.")],-1),y=s("ul",null,[s("li",null,"11 bits are used to store the position of the decimal dot within the number."),s("li",null,[e("52 bits, any whole number less than "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("msup",null,[s("mn",null,"2"),s("mn",null,"52")])]),s("annotation",{encoding:"application/x-tex"},"2^{52}")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},[s("span",{class:"mord"},"2"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},[s("span",{class:"mord mtight"},"52")])])])])])])])])])])]),e(", which is more than "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("mn",null,"1"),s("msup",null,[s("mn",null,"0"),s("mn",null,"15")])]),s("annotation",{encoding:"application/x-tex"},"10^{15}")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},"1"),s("span",{class:"mord"},[s("span",{class:"mord"},"0"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},[s("span",{class:"mord mtight"},"15")])])])])])])])])])])]),e(" will safely fit in a JavaScript number, numbers we are using stay well below that.")])],-1),b=a(`
Fractional numbers are written by using a dot,
js
9.81
9.81
1
For extreme huge or tiny numbers, we can also use "scientific" notion by adding an e, followed by the exponent of the number,
js
2.998e8
2.998e8
1
`,4),g=s("p",null,[e("This indicates "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("mn",null,"2.998"),s("mo",null,"×"),s("mn",null,"1"),s("msup",null,[s("mn",null,"0"),s("mn",null,"8")]),s("mo",null,"="),s("mn",null,"29980000")]),s("annotation",{encoding:"application/x-tex"},"2.998 \\times 10^8 = 29980000")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.7278em","vertical-align":"-0.0833em"}}),s("span",{class:"mord"},"2.998"),s("span",{class:"mspace",style:{"margin-right":"0.2222em"}}),s("span",{class:"mbin"},"×"),s("span",{class:"mspace",style:{"margin-right":"0.2222em"}})]),s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},"1"),s("span",{class:"mord"},[s("span",{class:"mord"},"0"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},"8")])])])])])])]),s("span",{class:"mspace",style:{"margin-right":"0.2778em"}}),s("span",{class:"mrel"},"="),s("span",{class:"mspace",style:{"margin-right":"0.2778em"}})]),s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.6444em"}}),s("span",{class:"mord"},"29980000")])])]),e(".")],-1),C=s("p",null,[e("Calculations with whole numbers (referred as "),s("em",null,"integers"),e(") that fits into 52 bits are guaranteed to be always precise, but calculation with fraction numbers are generally not.")],-1),v=s("ul",null,[s("li",null,[e("Such as "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("mi",null,"π")]),s("annotation",{encoding:"application/x-tex"},"\\pi")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.4306em"}}),s("span",{class:"mord mathnormal",style:{"margin-right":"0.03588em"}},"π")])])]),e(" cannot be precisely expressed by a finite amount of decimal digits, "),s("strong",null,"thus many numbers lose some precision when only 64 bits are available to store them.")])],-1),w=s("details",{class:"details custom-block"},[s("summary",null,"1-1-1: Numbers review"),s("ul",null,[s("li",null,[e("64 bits: A 64-bit number is a binary sequence of 64 bits, which can represent "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("msup",null,[s("mn",null,"2"),s("mn",null,"64")])]),s("annotation",{encoding:"application/x-tex"},"2^{64}")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},[s("span",{class:"mord"},"2"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},[s("span",{class:"mord mtight"},"64")])])])])])])])])])])]),e(" (18,446,744,073,709,551,616) different values. This is commonly used for representing memory addresses and integers with large values in computer systems.")]),s("li",null,[e("11 bits: An 11-bit number is a binary sequence of 11 bits, which can represent "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("msup",null,[s("mn",null,"2"),s("mn",null,"11")])]),s("annotation",{encoding:"application/x-tex"},"2^{11}")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},[s("span",{class:"mord"},"2"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},[s("span",{class:"mord mtight"},"11")])])])])])])])])])])]),e(" (2,048) different values. This is often used in computer systems for encoding small integers, such as color values in images.")]),s("li",null,[e("52 bits: A 52-bit number is a binary sequence of 52 bits, which can represent "),s("span",{class:"katex"},[s("span",{class:"katex-mathml"},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("semantics",null,[s("mrow",null,[s("msup",null,[s("mn",null,"2"),s("mn",null,"52")])]),s("annotation",{encoding:"application/x-tex"},"2^{52}")])])]),s("span",{class:"katex-html","aria-hidden":"true"},[s("span",{class:"base"},[s("span",{class:"strut",style:{height:"0.8141em"}}),s("span",{class:"mord"},[s("span",{class:"mord"},"2"),s("span",{class:"msupsub"},[s("span",{class:"vlist-t"},[s("span",{class:"vlist-r"},[s("span",{class:"vlist",style:{height:"0.8141em"}},[s("span",{style:{top:"-3.063em","margin-right":"0.05em"}},[s("span",{class:"pstrut",style:{height:"2.7em"}}),s("span",{class:"sizing reset-size6 size3 mtight"},[s("span",{class:"mord mtight"},[s("span",{class:"mord mtight"},"52")])])])])])])])])])])]),e(" (4,503,599,627,370,496) different values. This is often used for representing the significand or mantissa portion of a floating-point number in computer systems.")])])],-1),k=a(`
The main thing to do with numbers is arithmetic. Arithmetic operations such as addition or multiplication take two number values and produce a new number from them, the following is an example of calculation in JavaScript.
js
100+4*11
100+4*11
1
The + and * symbols are called operators.
+ apparently stands for addition while * stands for multiplication.
This expression indicates the multiplication takes place first then the adding of 100 comes next as a regard to PEMDAS rules (Order of Operations).
But we can still overwrite the steps of operation using a parenthesis around the addition,
js
;(100+4) *11
;(100+4) *11
1
For subtraction, there is the - operator, and division can be done with /. When operators appear together without parentheses, they are applied is determined by the precedence of the operators.
When multiple operators with the same precedence appear next to each other (as 1-2+1), they are applied left to right (refers to PEMDAS).
Precedence: Priorities.
WARNING
When is doubt, don't care about precedence, just add parentheses for order of operation.
There is one more arithmetic operator, which is the % percent sign used to represent the modulo operation. The term "X modulo Y" is defined as the remainder of dividing X by Y.
For example, 314 % 100 is 14, 10 % 3 is 1, and 144 % 12 is 0 because there are no remainders.
Modulo's precedence is the same as that of multiplication and division.
1-2: Arithmetic review
All of the operators in JavaScript follows precedence rules, or referred as PEMDAS in conventional math.
+ and - has the same lowest precedence.
* , / and % has the same medium precedence.
() has the highest precedence, followed on with brackets.
The symbol % refers to "modulo", in mathematical reading, we say "X modulo Y" is the remainder of X over Y.
The next data type is the string. Its use is not as evident from its name as with numbers, it also fulfills a very basic role.
Strings are used to represent text.
Strings are usually written by enclosing the contents with quotes.
js
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.''Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.''Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
1 2
TIP
Both single and double quotes can be used to mark strings
As long as the quotes at the start and the end of the string match.
Almost anything can be put between quotes, and JavaScript will make a string value out of it. But the followings are tricky to be put between quotes.
Newlines: The things we get when we press enter on keyboard. We uses \\n to represent.
New tab: Similarly like newlines, \\t indicates a new tab being indented.
Take the following string as an example,
js
'Lorem ipsum dolor sit amet,\\n consectetur adipiscing elit.'
'Lorem ipsum dolor sit amet,\\n consectetur adipiscing elit.'
1
The following will be the rendered output,
js
Lorem ipsum dolor sit amet,consectetur adipiscing elit.
Loremipsumdolorsitamet,consecteturadipiscingelit.
1 2
WARNING
But the situations where we want a backslash in a string to just be a backslash instead of a special code. By using \\\\ would render the "just slash" to \\ on output, instead of a special character, as follows,
js
"A newline character is written like \\"\\\\n\\"."
"A newline character is written like \\"\\\\n\\"."
1
Strings cannot be divided, multiplied, or subtracted, unlike integers. But the + operator can be used on the. It concatenates (glues) the two strings together, the following example will produce the string "concatenate".
js
'con'+'cat'+'e'+'nate'
'con'+'cat'+'e'+'nate'
1
1-1-3: Strings review
Nearly everything could be put into strings in JavaScript
String could be wrapped with "" double quotation marks or '' single quotation marks.
There is a tricky situation in JavaScript's string, where \\ backslash followed with a character are being introduced specially by the interpreter, such as \\n referred as newline, \\t referred as new tab.
\\\\ double backslash will be rendered as a single \\.
\\& symbols followed after slash will not contain its original function such as connecting, but will be rendered as & on output.
From the example given in the prior section with a lot of backslashes, it might be hard to understand the concept on at what time the slash will be rendered and when it will be considered as a special character starter.
The example is given as,
js
"A newline character is written like \\"\\\\n\\"."
"A newline character is written like \\"\\\\n\\"."
1
The rendered output will be on print,
js
A newline character is written like "\\n".
Anewlinecharacteriswrittenlike"\\n".
1
the backslash character (\\) is used as an escape character to indicate that the following character(s) should be treated specially. In this case, the "\\n" sequence is an escape sequence that represents a newline character. The backslash before the n character tells the JavaScript interpreter that it should treat the n as a special character and not just as the letter "n".
However, if we want to include an actual backslash character (\\), we will have to escape it by using to two backslashes \\\\, or else with one single slash the JavaScript interpreter will still determines it as an special operator, Thus \\\\ will print \\ on output.
But what about the quotation marks used within the quotation marks that wraps the string? Wouldn't JavaScript interpreter throw error?
In the given JavaScript string, the inner set of double quotation marks ("\\\\n") is escaped using a backslash (\\) character. This tells the JavaScript interpreter to treat the inner double quotation marks as a regular character instead of a string delimiter.
So when the string is rendered on output, the backslash character will be removed and the inner set of quotation marks will be displayed as a regular character. The rendered string will look like this,
js
'\\n'
'\\n'
1
The outer set of double quotation marks in the original string delimit the entire string and will be displayed as regular quotation marks.
Source: ChatGPT CA (Code Analysis)
`,37);function A(D,F,B,f,x,_){const n=p("Badge");return l(),o("div",null,[c,s("h2",d,[e("1-1-1: Numbers "),i(n,{type:"danger",text:"must know"}),e(),h]),u,m,y,b,g,C,v,w,k])}const E=t(r,[["render",A]]);export{T as __pageData,E as default};