Voice of a Developer: JavaScript Data Types

Introduction

 
JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript and what mistakes generally developers make and what differences they should be aware of. 
 

Data types

 
A very common word and try basic in a programming language, JavaScript provides different data types to hold different types of values. 
 

Q: How many data types are there in JS?

 
Ans: There are two types of data types in JavaScript.
 

Primitive data type

  • String
  • Number
  • Boolean
  • Undefined
  • Null

Non-primitive (reference) data type

  • Object
  • Array
  • RegExp

Q: How do you specify the data types in JS?

 
Ans: It’s simple and could be defined using var keyword only
 
Ex
  1. var x=10; // holds number    
  2. var x=”javascript”; // holds string   
As a developer, I am always curious to know the size of data types. But there is no sizeof function available which provides me size in JavaScript. But it’s good to know memory consumption when we work on bigger applications.
 
Download sizeof.js
 
Download one of the files below and upload it to your web server.
 
Example
  1. // define an object    
  2. var object = {  
  3.     'boolean'true,  
  4.     'number': 1,  
  5.     'string''a',  
  6.     'array': [1, 2, 3]  
  7. };  
  8. // determine the size of the object    
  9. var size = sizeof(object);  
  10. //output: size is 92 bytes 

Q: What is the difference between Undefined and Null?

 
Ans: In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such a,
  1. var TestVar;  
  2. alert(TestVar); //shows undefined    
  3. alert(typeof TestVar); //shows undefined    
  4. null is an assignment value.It can be assigned to a variable as no value: var TestVar = null;  
  5. alert(TestVar); //shows null    
  6. alert(typeof TestVar); //shows object 

Objects

 
Objects are variables too. However, objects can contain many values.
 
Ex
  1. var person = {age:59, address:"Delhi”};   

Q: Do you know a simple way to create objects?

 
Ans
  1. var obj = {};  
  2. //undefined    
  3. typeof(obj);  
  4. "object"  
  5. Object Properties  
  6. You can access object properties in two ways: objectName.propertyName  
  7. or  
  8. objectName["propertyName"
Ex
  1. person.age     
  2. Or    
  3. Person[“age”]   

Object Methods

 
You access an object method with the following syntax:
 

objectName.methodName()

 
Example
  1. var person = {  
  2.     age: 59,  
  3.     address: "Delhi",  
  4.     fullName: function() {  
  5.         console.log('John Doe');  
  6.         return 'John Doe';  
  7.     }  
  8. }; 
If you access the property without parenthesis (), it will return the function definition:
 
person.fullName
  1. // function () {console.log('John Doe'); return 'John Doe';}    
  2. Otherwise it’ ll execute the method  
  3. person.fullName();  
  4. // John Doe 

Loop over all properties in Object

 
We often use FOR statement to loop, but there is another feature, for in,  which is pretty useful to iterate over all properties of Object
 
Ex
  1. for(a in person){console.log(a)}  
  2. age  
  3. address  
  4. fullName  

Q: How do you add/delete properties to existing object?

 
Ans: It is simple in Javascript, by giving a value
 
Ex
  1. person.country = “India”;   

To delete you could use the delete keyword

 
Ex
  1. delete person.age; 

Access operator

 
Goto browser console window and try to access all properties /methods via access operators, ex-
 
OPERATOR
 

Q: Can you access a property, which does not exist in Object?

 
Ans: Yes, you can access a property on an object that does not exist but will result is undefined.
 
Ex
  1. person.lastName;    
  2. //undefined   

Q: Can you access a method, which does not exist in Object?

 
Ans: No, you cannot access a method on an object that does not exist because it’ll yield an error.
 
Ex
  1. person.lastName();   
 
person
 
Read more articles on JavaScript


Similar Articles