JavaScript has several built-in data types that you can use. Here are the commonly used data types in JavaScript:
Number: Represents numeric values, including integers and floating-point numbers.
let age = 25;
let pi = 3.14;
String: Represents textual data enclosed in single quotes (') or double quotes (").
let name = 'John';
let message = "Hello, world!";
Boolean: Represents either true or false, indicating the truth value of an expression.
let isTrue = true;
let isFalse = false;
Object: Represents a collection of key-value pairs, where values can be of any data type. Objects are denoted by curly braces {}.
let person = {
name: 'John',
age: 30,
city: 'New York'
};
Array: Represents an ordered list of values, which can be of any data type. Arrays are denoted by square brackets [].
let numbers = [1, 2, 3, 4, 5];
let fruits = ['apple', 'banana', 'orange'];
Null: Represents the intentional absence of any object value. It is a primitive value that denotes the absence of an object reference.
let data = null;
Undefined: Represents an uninitialized or undefined value. It is typically the default value of variables that have been declared but not assigned a value.
let variable;
console.log(variable); // Output: undefined
Symbol: Represents a unique identifier. Symbols are often used as keys in objects to avoid naming conflicts. Symbols are created using the Symbol() function. Introduced in ECMAScript 6 (ES6).
const id = Symbol('id');
let obj = {
[id]: '12345'
};
BigInt: Represents integers with arbitrary precision. It allows you to work with numbers beyond the Number data type's limit. BigInts are created by appending n to the end of an integer literal or by using the BigInt() constructor. Introduced in ECMAScript 2020.
let bigNumber = 1234567890123456789012345678901234567890n;
HTML
<template>
<lightning-card>
<p>String: {stringExample}</p>
<p>Number: {numberExample}</p>
<p>Boolean: {booleanExample}</p>
<p>Object: {objectExample}</p>
<p>Array: {arrayExample}</p>
</lightning-card>
</template>
Java Script
import { LightningElement } from 'lwc';
export default class DataTypeExample extends LightningElement {
// Define some different data types
stringExample = 'Hello, world!';
numberExample = 42;
booleanExample = true;
objectExample = {
name: 'John Doe',
age: 30,
email: 'johndoe@example.com'
};
arrayExample = [1, 2, 3, 4, 5];
}
Video
Video does not exists.
Mastered by 15 users.