Cover Image for Javascript Data Types
253 views

Javascript Data Types

JavaScript is a loosely typed language, which means that data types are not explicitly declared. Instead, JavaScript will automatically convert data types as needed during runtime. Here are the commonly used data types in JavaScript:

  1. Number: represents numeric values. Examples include 3, 3.14, -12, and Infinity.
  2. String: represents a sequence of characters. Examples include “Hello, World!”, “JavaScript is fun”, and ‘a’.
  3. Boolean: represents a logical value that can be either true or false.
  4. Undefined: represents a value that is not yet defined. If a variable is declared but not initialized, its value will be undefined.
  5. Null: represents a deliberate non-value. It is usually assigned to a variable as a representation of no value or an empty value.
  6. Object: represents a collection of properties, where each property is a key-value pair. Objects can contain functions, arrays, and other objects.
  7. Symbol: represents a unique identifier that can be used as a key in an object.

Here’s an example of defining and using different data types in JavaScript:

JavaScript
// Number
let num = 10;
console.log(num); // Output: 10

// String
let str = "Hello, World!";
console.log(str); // Output: Hello, World!

// Boolean
let isTrue = true;
console.log(isTrue); // Output: true

// Undefined
let myVar;
console.log(myVar); // Output: undefined

// Null
let myNull = null;
console.log(myNull); // Output: null

// Object
let obj = {
name: "John",
age: 30,
hobbies: ["reading", "coding"]
};
console.log(obj.name); // Output: John

// Symbol
let mySymbol = Symbol("mySymbol");
console.log(mySymbol); // Output: Symbol(mySymbol)

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS