Learning ES6 for React: New Array Methods

Learning ES6 for React: New Array Methods

1 Like

Difference between primitive types and reference types

primitive - These data types are pretty simple, and are sometimes treated as the lowest level of implementation of a programming language. They are not objects, and do not have methods.

Examples of such data types are numbers, strings, Booleans, null, and undefined.

reference - Reference data types, unlike primitive data types, are dynamic in nature. That is, they do not have a fixed size.

Most of them are considered as objects, and therefore have methods. Examples of such data types include arrays, functions, collections, and all other types of objects.

difference between reference and primitive data types lies in how they store and handle data. Primitive data types directly contain the data, are usually smaller, and are passed by value. Reference data types store memory addresses, are more complex, and are passed by reference (i.e., the memory address is passed). Each type has its advantages and use cases, depending on the requirements of the program.

Primitive type-> This is a data types is very simple and include the numeric types (Double, int, char).
ex-Numbers, Boolean, Null, undefined.
Reference type-> This is data types is unlike the primitive type and it is not fixed.
ex- Object, function, array.

Edge cases with Reduce methods ? ******

const arr = [116, 190, 83, 46]; // 46, 83, 116, 190

arr.sort((a, b) => {
return b - a;
});

console.log(arr); // [ 46, 83, 116, 190 ] // asc

// [ 190, 116, 83, 46 ] // desc

primitive:These data types are pretty simple, and are sometimes treated as the lowest level of implementation of a programming language. They are not objects, and do not have methods.
Examples of such data types are numbers, strings, booleans, null, and undefined.

reference type:Reference data types, unlike primitive data types, are dynamic in nature. That is, they do not have a fixed size.

Most of them are considered as objects, and therefore have methods. Examples of such data types include arrays, functions, collections, and all other types of objects.

The difference comes in when the computer has to store a reference data type. When you create a variable and assign it a value that is a reference data type, the computer does not directly store that data type in that variable (as is the case with primitive types).

What we have assigned to that variable is a pointer that points to the location of that data type in memory.

Primitive Types: Primitive types are simple data types that hold a single value. In JavaScript, there are six primitive types: number , string , boolean , null , undefined , and symbol . When you work with primitive types, you directly manipulate the actual value stored in memory.

Reference Types:Reference types, on the other hand, are more complex data types that can hold multiple values and are stored as references (memory addresses) to their actual values. In JavaScript, reference types include **object , array , function** , and others. In React, most complex data structures, like component instances or state objects, are reference types.

1 Like

data types can be categorized as mutable or immutable based on whether their values can be changed after they are created.
In computer programming, data types can be categorized as mutable or immutable based on whether their values can be changed after they are created.

Mutable Data Types: Mutable data types are those whose values can be modified or changed. This means that you can update, add, or remove elements within these data types without creating a new instance.
Example: Reference datatype: Object , Array etc.

Immutable Data Types:
Immutable data types, on the other hand, are those whose values cannot be changed once they are created. If you want to modify the value, you need to create a new instance of the data type.
Example: primitive datatype: Number , String , Boolean , Null , Undefined , Symbol etc. these are six.

Difference between primitive and reference types:
The difference between primitive datatypes and reference datatypes lies in how they are stored and accessed in memory. the key difference is that primitive datatypes store the value directly, while reference datatypes store a reference to the value or object. This difference affects how memory is allocated, how operations are performed, and how variables are passed to functions.
Primitive Datatypes: Primitive datatypes, also known as value types, represent basic data types in a programming language. They are usually predefined by the language and have a fixed size. Primitive datatypes store the actual value of the variable directly in memory. Examples of primitive datatypes include integers, floating-point numbers, characters, and booleans.
Characteristics of primitive datatypes:

  • They have a fixed size and are stored directly in memory.
  • Operations on primitive datatypes generally involve manipulating the value itself.
  • Primitive datatypes are passed by value when used as function arguments.

Reference Datatypes: Reference datatypes, also known as objects or reference types, store references or memory addresses that point to the actual data stored elsewhere in memory. They are usually created using class definitions or built-in classes. Reference datatypes have a variable size, which depends on the size and complexity of the object being referred to. Examples of reference datatypes include arrays, strings, and user-defined classes.
Characteristics of reference datatypes:

  • They store a reference to the actual data in memory, rather than the data itself.
  • Operations on reference datatypes involve manipulating the reference or accessing/modifying the data indirectly through the reference.
  • Reference datatypes are passed by reference (or by value of the reference) when used as function arguments.
1 Like

Reduce method in JavaScript are situations where the initial value, the accumulator function, or the data being reduced can lead to unexpected or erroneous behavior

Here are some edge cases

No Initial Value with an Empty Array.
Non-Array Data.
Using Object as Initial Value.
Modifying the Initial Value.

1 Like

An edge case is a problem or situation that occurs only at an extreme (maximum or minimum) operating parameter — you can think of these as special cases that you need to account for.
Edge cases for reduce function
if we are not giving initial value to reduce function , accumulator will take first value of array as initial value and also index will start from one not zero.

If the array only has one element (regardless of position) and no initialValue is provided, or if initialValue is provided but the array is empty, the solo value will be returned without calling callbackFn.

If initialValue is provided and the array is not empty, then the reduce method will always invoke the callback function starting at index 0.

If initialValue is not provided then the reduce method will act differently for arrays with length larger than 1, equal to 1 and 0,

1 Like

Lexicographic sorting, also known as dictionary ordering or alphabetical sorting, is a way of ordering elements based on their lexical order, similar to how words are ordered in a dictionary. In lexicographic sorting, elements are compared character by character from left to right, and the comparison is based on the Unicode values of the characters. lexicographic sorting is the default behavior when sorting elements that are not explicitly converted to numbers. When sorting numbers lexicographically, the elements are treated as strings, and the sorting is performed based on their Unicode values character by character.

But for above example sorting is done by using an comparison function to avoiding this lexicographic sorting for sort function
The expression sort((a, b) => b - a) represents a custom comparison function used with the sort() method. It is commonly used to sort numbers in descending order.

Let’s break down the components of the expression:

  • (a, b) => b - a: This is an arrow function that takes two parameters, a and b. It subtracts a from b, resulting in a numeric value. The function returns this value, indicating the desired order for sorting.
  • b - a: The subtraction b - a is performed to determine the relative order of the elements. By subtracting a from b, the function produces a negative value if b should come before a (descending order).

When this custom comparison function is passed to the sort() method, it instructs the method to sort the elements in descending order based on their numeric values. The sort() function uses the returned values from the comparison function to determine the final order of the elements.

1 Like

Primitive Data types:
Primitive data type represent simple value and values cannot be changed after they created.
Examples of such data types are numbers, strings, booleans, null, and undefined,Symbols.
Primitive data type are copied by value.
When you assign a primitive value from one variable to another, the JavaScript engine creates a copy of that value and assigns it to the variable. If you change the value of one variable, it won’t affect the other.
Reference Data Types:
Reference data types, unlike primitive data types, are dynamic in nature.
Values cannot be modified after they created.
Examples of such data types include arrays, functions, collections, and all other types of objects.
Reference data type are copied by reference.
When you assign a reference value from one variable to another, the JavaScript engine creates a reference so that both variables refer to the same object on the heap memory. This means that if you change one variable, it’ll affect the other.

1 Like

Primitive data types-A primitive variable’s information is stored as the value of that variable,

Reference data tpye- whereas a reference variable holds a reference to information related to that variable.

1 Like

1 Like

Edge cases with Reduce methods:
*Functional sequential piping
*Running promises in sequence
*using reduce() with sparse arrays
*calling reduce() with non array objects
*When not to use reduce()

1 Like

const arr = [1, 2, 3, 4, 5, 5, , 6];

const a = arr.map((x, y) => x + y);

console.log(a);

someone can define please it’s output?

1 Like

Primitive data type:-
These data types are treated as the lowest level of implementation of a programming language.They are not objects, and do not have methods.
eg- numbers,string,boolean,null,undefined and symbols.

Reference data types:-
These data types are dynamic in nature thats why they do not have a fixed size.Most of them are considered as objects and therefore have methods.
eg- objects,functions,collections,arrays,dates,etc

1 Like