JavaScript data types and values
JavaScript has typed values, not typed variables. JavaScript supports several data types and values. Here are some of the most commonly used ones:
Primitive Data Types:
JavaScript has 6 primitive data types:
- Number: used to represent numeric values, both integers and floating-point numbers.
- String: used to represent textual data.
- Boolean: used to represent true/false values.
- Null: used to represent a deliberate non-value or empty value.
- Undefined: used to represent a value that has not been defined.
- Symbol: introduced in ECMAScript 6, used to represent a unique identifier.
Examples
let myNumber = 42;
let myString = “Hello, world!”;
let myBoolean = true;
let myNull = null;
let myUndefined = undefined;
let mySymbol = Symbol(“mySymbol”);
We can use the typeof operator to examine a value in order to tell what type it is:
- var myVariable;
typeof myVariable; // “undefined”
- myVariable = “hello, world”;
typeof myVariable; // “string”
- myVariable = 50;
typeof myVariable; // “number”
- myVariable = true;
typeof myVariable; // “boolean”
- myVariable = null;
typeof myVariable; // “object”–weird, bug
- a = undefined;
typeof a; // “undefined”
- myVariable = { hobby: “reading” };
typeof myVariable; // “object”
- Notice how myVariable holds every different type of value in Js. This is because the typeof operator is not asking for the type of myVariable, but rather the type of the value currently in myVariable.
- Only values have types in JS, variables are just simple containers for those values.
- Also typeof null erroneously returned object instead of null. This is an old bug that has never been fixed till date, probably not fixed to avoid breaking codes that depend on the bug.
- Finally, we did explicitly assigned the undefined value to our variable. That is exactly the same as having a variable with no value. Variables can end up in the undefined state in several ways such as having functions that return no value or using the void operator.
Objects:
- Objects are used to store collections of data and functionality.
- They consist of key-value pairs, where the key is a string (or symbol) and the value can be any data type.
- Objects can also contain methods, which are functions that belong to the object.
Examples:
let myObject = {
name: “John”,
age: 30,
isStudent: true,
sayHello: function() {
console.log(“Hello, my name is ” + this.name);
}
};
console.log(myObject.name); // Accesses the “name” property of the “myObject” object
myObject.sayHello(); // Calls the “sayHello” method of the “myObject” object
Arrays:
- Arrays are used to store collections of data, and are similar to objects, except that their keys are automatically assigned numeric indices. Arrays can contain any data type, including other arrays and objects.
Examples:
let myArray = [“apple”, “banana”, “orange”];
console.log(myArray[0]); // Accesses the first element of the “myArray” array
myArray.push(“pear”); // Adds a new element to the end of the “myArray” array
Functions:
- Functions are used to encapsulate code and perform specific tasks. They can take parameters and return values, and can also be assigned to variables, passed as arguments to other functions, and returned as values from other functions.
Examples:
function addNumbers(num1, num2) {
return num1 + num2;
}
let result = addNumbers(10, 5); // Calls the “addNumbers” function and assigns the result to the “result” variable
These are just a few of the most commonly used data types and structures in JavaScript. There are many more, including regular expressions, dates, and maps, among others.