In this tutorial, let's discuss some data types in javascript and some javascript browser / runtime types that support use with these data types. Please note that javascript is a dynamic programming language for data types, so javascript automatically converts directly to string, integer, or < b>float.
1. let
This data type can be used for a variable that is mutable (mutable), and cannot be re-declared. An example of its use is as follows
let a = 20;
a+=20;
console. log(a); //the result is 40
This data type can change, according to your needs.
Browser support by caniuse.com
2. const
The data type const is an immutable data type (immutable).
const a = 20;
a+=20; // error -> Cannot assign to "a" because it is a constant
console. log(a);
Browser support by caniuse.com
3. var
This data type is similar to let. Where the variable for this data type can be (mutable). However, this data type can be stacked with the same data type even though it has been described (can be re-declared). Example:
var a = 10;
vara = 20; //doesn't cause an error
console. log(a); //output is 20
4. integer(num)
The integer data type will automatically be detected by javascript. This data type is an integer number (no commas).
example:
const a = 20+10
console. log(a); //result is 30
Support All Browsers / runtime types
5. Float(num)
The float data type will automatically be detected by javascript. This data type is a number that has fractions.
example:
const a = 10/20
console. log(a); //result is 0.5
Support All Browsers / runtime types
6. Strings
The string data type will automatically be detected by javascript. This data type is a series of letters, other punctuation marks, and numbers.
example:
const a = "Gilang Pratama"
console. log(a);
Support All Browsers / runtime types
7. Functions
The function data type will automatically be detected by javascript. This data type is a data type that can run a logic. This data type is commonly used as a callback. Let's look at this slightly tricky example.
Examples of Common Functions
const sum = (a, b)=>{
return a+b;
}
const sum = sum(2,3)
console.log(count) //output is 5
Example of Function Callback
const sum = (a, b)=>{
return a+b(); //b() is the way to run the function
}
const sum = sum(2,
//here declare callback function
()=>{
return 40 //here returns the number 40
})
console.log(count)//output is 42
Support All Browsers / runtime types
8. Promises
This data type is automatically detected by javascript as a asynchronous object. In javascript, asynchronous is a way of executing without blocking other processes.
Sample Promise:
const main = async ()=>{ //must use async to await data
const area = await loop(7)
console.log(width)
}
const circle = (radius)=> new Promise((resolve, reject)=>{
resolve(Math.round(radius*radius*3.14)) //to return the data
})
main();
Browser support by caniuse.com
9. Array (Heap of data)
Arrays are also automatically detected by javascript. Arrays are used to write stacked data.
Example:
const cars = ['mitsubisi', 'honda'];
console.log(cars)//(2) ["mitsubisi", "honda"]
Support all browsers / runtime types
10. Objects
Object is data that dynamically declares a key.
Example
const car = {
body: 'red',
wheels: 'black'
}
console. log(car) //(2) { body: "red", wheel: "black"}
Support all browsers / runtime types