All programming languages follow similar patterns but with different syntaxes but what stays constant is the data types. For this part of the class, we will focus more on the aspect of javascript data types.
In programming, a data type is a classification of data that determines the operations that can be performed on it, the meaning of the data, and the way it is stored in memory. Below are some common data types in JS;
Numbers:
Integer: A whole number without a decimal point, such as 3, -15, or 0.
Floating-point: A number with a decimal point, such as 3.14, -0.5, or 100.0
//Numbers let size_in_meters = 20; let size_in_inches = 2.1024 let maxSize = Infinity let minSize = -Infinity let not_a_number = NaN let sum = 20 + 23.1; let difference = 20 - 10; let multiplication_result = 20 * 10; let division_result = 20 / 2;
Strings:
A sequence of characters, such as "Hello, world!", 'Hello, world!', or "42".
let name = "Ayodeji Falz" let name2 = 'Ayodeji Remi' let name3 = `Ayodeji Azeez` let firstName = 'Ayodeji' let lastName = 'Azeez' // lets concatenate varaible firstname and lastname by storing them in another variable fullname let fullName = `Mr ${firstName}${lastName}`; alert(fullName); let message = "Hello my friends \n My name is Johnson\n I am a software engineer" // introduction of \n enables statement of codes to be broken into a new lines console.log(message.toUpperCase()); //the toUpperCase() methods converts characters to uppercases alert(message.toUpperCase()); // 'HELLO MY FRIENDS\n MY NAME IS JOHNSON\n I AM A SOFTWARE ENGINEER'
Boolean:
A data type that represents either true or false.
let message = "Hello my friends \n My name is Johnson\n I am a software engineer"
console.log(message.startWith("Hello"));
// true
// in this case the boolean result will be true because the method startWith("Hello") is valid
let isBigSize = true //Yes, the size is big
let sizeIsSmall = false //No, the size is not small
Null type:
A data type that indicates the absence of a value
//Null type let size = null let num = null
- Undefined:
The "undefined" data type is used to represent a variable or value that has not been assigned a value. It is typically used as the default value of a variable before it is initialized, or to indicate that a function has no return value.
let weight; //The variable size is undefined by default
let age = undefined //This can be done explicitly as well (Not recommended)
let myVariable = undefined;
console.log(myVariable); // output: undefined
function myFunction() {
return undefined;
}
console.log(myFunction()); // output: undefined
Objects:
In JavaScript, an object is a collection of key-value pairs where each key is a string (or symbol) and each value can be of any data type, including other objects. Objects in JavaScript are similar to objects in real life, in that they can have properties and methods.
//Objects let user1 = {} let user2 = new Object() user1["name"] = "Azeez" let user3 = { name: "John Doe Moses", age: 20, weight: 20.33333, "verifiedUser": true } let user = { name: "John Doe", age: 20, weight: 20.3, "verifiedUser": true } console.log(user3["name"]); console.log(user.age); console.log(user3.weight); delete user3.weight;
These are some examples of the data types in Javascript, ensure you understand how these data types are used.
May the forces be with you, Goodluck.