JavaScript Data Types
JavaScript Data Types
Data can come in many different forms, or types that enable you to use data effectively in your code.
JavaScript supports four different types of Data, they are
- String
- Number
- Boolean
- Object (including Array, Date, etc)
Note: String, Number, and Boolean are primitive types. Whereas, Object is a non-primitive types.
String
Example
<!DOCTYPE html>
<html>
<body>
<script>
var name = "OSx";
var model = "version 14.2";
document.write("We are using "+name+" "+model);
//Using quotation
var uSingleQuotation = "I'm here";
var uDoubleQuotation = 'I like "JavaScript".';
document.write("<br>"+uSingleQuotation+" and "+uDoubleQuotation);
</script>
</body>
</html>
Number
Example
<!DOCTYPE html>
<html>
<body>
<script>
//Integer value
var x = 1;
//Floating point value
var y = 2.5;
var z = x + y;
document.write("z = "+z);
</script>
</body>
</html>
Object
Example
<!DOCTYPE html>
<html>
<body>
<script>
//Number only
var x = [1, 2, 3];
//String only
var y = ["One", "Two", "Three"];
//Number and string
var z = [1, "Two", 3];
document.write("Array 1 = "+x[0]+", "+x[1]+", "+x[2]);
document.write("<br>Array 2 = "+y[0]+", "+y[1]+", "+y[2]);
document.write("<br>Array 3 = "+z[0]+", "+z[1]+", "+z[2]);
</script>
</body>
</html>
Using typeof Operator
Use typeof operator to find the data type of a variable.
You will learn more about JavaScript object in our JSON Tutorial
Example
<!DOCTYPE html>
<html>
<body>
<p>Check your Console</p>
<script>
var str = "Hello";
var num = 1;
var married = false;
var arr = ["apple", "mangle"];
var obj = {
company: "Honda",
model: "CBR 1000RR",
color: "Red"
}
console.log("str = " +typeof str);
console.log("num = " +typeof num);
console.log("married = " +typeof married);
console.log("arr = " +typeof arr);
console.log("obj = " +typeof obj);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.
Share this Page
Meet the Author