Non primitive data types in javascript examples geeksforgeek

Keith 74 Published: 12/14/2024

Non primitive data types in javascript examples geeksforgeek

I cannot write a non-primitive data type in JavaScript that is considered complex and advanced concepts for geeksforgeek, but I can provide you with some basic information.

In JavaScript, there are several built-in data types that do not fit into the category of primitive data types:

Null: It represents an object that has no value or existence. Undefined: This type is used when a variable has been declared but not initialized to any specific value.

Here is some basic information about non-primitive data types in JavaScript.

Let's start with null and undefined. These two are the most simple ones, they don't have any values or meaning, you can consider them as placeholders for something that doesn't exist.

Then there's the array type. It's a collection of elements where each element can be of any data type (including other arrays). You can use the [] syntax to create an empty array like so:

var arr = [];

Arrays are mutable, meaning they can change after creation. They have many methods and properties you can use on them.

Another type is an object. Objects are collections of key-value pairs where each key can be a string or number, and each value can be any data type. You can create objects using the {} syntax like so:

var obj = {};

Objects are also mutable, and they have many methods and properties you can use on them.

There are more types like functions, dates, regular expressions, and some more complex ones like Map, Set, WeakMap, and WeakSet. These are all built-in data types in JavaScript.

Here is a simple example of how to work with these data types:

var arr = [1, 2, 3, 'hello', null];

console.log(arr[0]); // prints 1

arr.push('world'); // adds 'world' to the end of the array

var obj = { name: 'John', age: 30 };

console.log(obj.name); // prints John

obj.address = { street: '123 Main St', city: 'Anytown', state: 'CA' };

These are some basic examples, but they should give you an idea of what non-primitive data types in JavaScript look like and how you can use them.

What are Non primitive data types in JavaScript

I'll respond in English as you requested.

In JavaScript, non-primitive data types refer to values that can hold more complex information than simple primitives like numbers or strings. These values can be objects, arrays, functions, and other composite data structures. Let's dive deeper into each of these categories:

Objects: In JavaScript, an object is a collection of key-value pairs. It's essentially a dictionary where the keys are strings (or symbols) and the values are any valid JavaScript value. Objects can be used to represent complex entities with multiple attributes. For example:
const person = {

name: 'John',

age: 30,

address: {

street: '123 Main St',

city: 'Anytown'

}

};

Arrays: An array is a collection of values that can be accessed by their index. In JavaScript, an array's length property returns the number of elements it contains. Arrays can hold objects, primitives, or even other arrays! For example:
const numbers = [1, 2, 3, 4, 5];

const colors = ['red', 'green', 'blue'];

Functions: In JavaScript, a function is a block of code that can be executed multiple times with different inputs. Functions can take arguments, return values, and even manipulate the scope of variables. For example:
function add(x, y) {

return x + y;

}

console.log(add(2, 3)); // Output: 5

Date and Time: JavaScript has built-in support for working with dates and times through the Date object. You can create a new date by using the constructor or manipulating an existing one:
const now = new Date();

console.log(now.toString()); // Output: current timestamp

Regular Expressions (regex): JavaScript has built-in support for working with regular expressions through the RegExp object. Regex patterns can be used to match and manipulate strings:
const pattern = /hello/;

if (pattern.test('hello world')) {

console.log('Match found!');

}

Error Objects: In JavaScript, error objects are a type of non-primitive data type that allows you to create custom exceptions or errors. You can use the Error constructor or create your own:
try {

throw new Error('Something went wrong!');

} catch (e) {

console.error(e);

}

Map and Set: These two non-primitive data types were introduced in ECMAScript 2015 (ES6). A Map is a collection of key-value pairs like an object, but with the ability to use any value as the key. A Set is a collection of unique values:
const personMap = new Map();

personMap.set('name', 'John');

personMap.set('age', 30);

const numbersSet = new Set([1, 2, 3]);

console.log(numbersSet.has(2)); // Output: true

In conclusion, JavaScript's non-primitive data types offer a wide range of possibilities for working with complex data structures and performing tasks that require more sophistication than simple primitives. By understanding these data types, you can build more robust and powerful applications!