This tutorial is a part of the Learn everything about Javascript in one course.
What is a data structure?
You might recall our ultimate purpose is to process and return useful information. Buit-in data structure for frequently used information type is one way Javascript helps to do this quickly.
A buit-in data structure in a programming language is a name to refer to frequently used information that has similar:
- presentation (to both for human and computer). For example: numbers or texts.
- manipulation methods to process this kind of information. For example: Maths on numbers.
We will learn more popular Javascript built-in data structures as well as building our own (to fit our special needs) in future lectures.
Declare number variable with literal (input)
We can declare variable and assign number value to it in one statement. These are our inputs in IPO.
let num1 = 1 // declare variable name `num1` and assign value 1 to it
let num2 = 2 // integer number
let floatNum = 3.4 // floating point number
let expNum1 = 5.6e+4 // exponential notation, equals 5.7e4 or 57000
let expNum2 = 5.6e-4 // exponential notation, equals 0.00056
Manipulate number variable with math (process)
You can do any manipulation with your number with Mathematics that you've known. We will learn more about these methods in later lectures.
...
// declare variable name `num3` and assign its value to be sume of `num1` and `num2`
let num3 = num1 + num2
Return some useful information (output)
...
// print them out in console
console.log('num1:', num1) // num1: 1
console.log('num2:', num2) // num2: 2
console.log('num3:', num3) // num3: 3
console.log('expNum1:', expNum1) // expNum1: 56000
console.log('expNum2:', expNum2) // expNum2: 0.00056
Global object in Javascript
Object
is a data structure in Javacript, we'll learn more about it later. Global object means the object is available and can be used immediately anywhere without declaring.
// `console` is a global object, we use it all the time without declaring
console.log('hello world')
console.log() is the most useful method for learning
You can use console.log()
anywhere to print out your data and inspect them.
...
// console.log method can take in multiple inputs and print them all out
// in this example
// console.log takes in two inputs, separated by a comma `,`
// first input is a literal: 'value of myVar is:'
// second input is a variable: myVar
let myVar = 1
console.log('value of myVar is:', myVar) // value of myVar is: 1
Object method
Is a function belongs to that object. This function process and may return something. We invoke a object method by objectName
+ .
+ methodName
+ (input)
.
...
// in this example
// console -> objectName
// log -> methodName
// 'hello world' -> input
console.log('hello world') // hello world
Object property
Is data belongs to that object. This data can be accessed by objectName
+ .
+ propertyName
.
...
// in this example
// Number -> objectName
// MAX_VALUE -> propertyName
console.log(Number.MAX_VALUE) // 1.7976931348623157e+308
Convert number to string
We can convert almost any data structures in Javascript to string. This holds true with number. A typeof
operator can be used to put in front of any variable to see what data structure that varible is.
...
// convert num1 to a string and assign that value to strNum1 variable
let strNum1 = num1.toString()
console.log('strNum1:', strNum1) // strNum1: 1, this `1` is a string
console.log('num1:', num1) // num1: 1, this `1` is a number
console.log('typeof num1:', typeof num1) // typeof num1: number
console.log('typeof strNum1:', typeof strNum1) // typeof strNum1: string
Min, max and safe presentation of Number
Global Number object contains important information of Number data structure:
...
Number // global Number object, ready to use anywhere, anywhen
// The smallest interval between two representable numbers.
console.log(Number.EPSILON) // 2.220446049250313e-16
// Integer bigger than this value will result inaccuracy in calculation.
console.log(Number.MAX_SAFE_INTEGER) // 9007199254740991
// Integer smaller than this value will result inaccuracy in calculation.
console.log(Number.MIN_SAFE_INTEGER) // -9007199254740991
// Max number can be presented in Javascript
console.log(Number.MAX_VALUE) // 1.7976931348623157e+308
// Min number can be presented in Javascript (without actually being zero)
console.log(Number.MIN_VALUE) // 5e-324
// "Not a Number" value. You will see this a lot actually.
// This usually tells that calculation can't be done with given inputs
console.log(Number.NaN) // NaN
console.log('a'/ 2) // NaN, a tring devide by 2 is not a number
// "-Infinity" represent the negative infinity number
console.log(Number.NEGATIVE_INFINITY) // -Infinity
console.log(-1 / 0) // -Infinity, -1 divide by 0 result -Infinity
// "Infinity" represent the positive infinity number
console.log(Number.POSITIVE_INFINITY) // -Infinity
console.log(1 / 0) // Infinity, 1 divide by 0 result Infinity
Convert string to number
with Number global object:
parseFloat()
: convert string to floating point (decimal) numberparseInt()
: convert string to integer
...
// convert string to integer number
// parseInt() and Number.parseInt() are the identical and global
let str = '3'
let num = parseInt(str) // convert `str` to number and assign it to num variable
let num4 = Number.parseInt(str) // convert `str` to number and assign it to num variable
console.log(typeof str, str) // string 3
console.log(typeof num, num) // number 3
console.log(typeof num4, num4) // number 3
// convert string to float (decimal) number
// parseInt() and Number.parseInt() are the identical and global
let str2 = '3.456'
let num5 = parseFloat(str2) // convert `str` to number and assign it to num variable
let num6 = Number.parseFloat(str2) // convert `str` to number and assign it to num variable
console.log(typeof str2, str2) // string 3
console.log(typeof num5, num5) // number 3
console.log(typeof num6, num6) // number 3
Full list of properties and methods of Number object can be found in MDN.
Summary
- Number data structure in Javascript helps to represent real life number.
- What you can do with real life number, you can do the same with Javascript.
- Javascript numbers have limitations:
max
,min
,safe
.