In this article we will explore top fundamental Javascript concepts necessary to know in order to have an effective first learning cycle of React / React Native
Table of Contents
map() & filter()
slice() & splice()
concat()
find() & findIndex()
destructuring
rest & spread operator
promises
map and filter
Both are array methods and both return a new array when applying Filter additionally eliminates items that don't match
map:
const Data =[
{id: '1',title: "car"},
{id: '2',title: "bus"},
{id: '3',title: "plane"},
{id: '4',title: "train"},
{id: '5',title: "ship"},
]
const upperData = Data.map(element => element.title.toUpperCase());
console.log(upperData)
Output:
['CAR', 'BUS', 'PLANE', 'TRAIN', 'SHIP']
filter:
const Data =[
{id: '1',title: "car"},
{id: '2',title: "bus"},
{id: '3',title: "plane"},
{id: '4',title: "train"},
{id: '5',title: "ship"},
]
const filterData = Data.filter(element => element.id %2 === 0);
console.log(filterData)
Output:
[ { id: '2', title: 'bus' }, { id: '4', title: 'train' } ]
slice and splice
Method returns a new array with selected elements, while splice method changes the contents of an existing array
splice:
const Data =[
'Car',
'Bus',
'Helicopter',
'Train'
]
const CopyArray = [...Data]
CopyArray.splice(0,1)
console.log(CopyArray)
Output:
['Bus', 'Helicopter', 'Train']
const Data =[
'Car',
'Bus',
'Helicopter',
'Train'
]
const CopyArray = [...Data]
CopyArray.splice(CopyArray.length,1,"Plane")
console.log(CopyArray)
Output:
['Car', 'Bus', 'Helicopter', 'Train', 'Plane']
slice:
const Data =[
'Car',
'Bus',
'Helicopter',
'Train'
]
const CopyArray = [...Data]
const newArray = CopyArray.slice(0,2)
console.log(newArray)
console.log(Data)
Output:
['Car', 'Bus']
['Car', 'Bus', 'Helicopter', 'Train']
concat
This method returns a new array of merging two or more arrays
concat:
const array1 = [1, 2, 3, 4, 5];
const array2 = [6, 7, 8, 9, 10];
const array3 = [11, 12, 13, 14, 15];
const mergeArray = array1.concat(array2, array3);
console.log(mergeArray);
Output:
[
1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15
]
find and findIndex
The find method returns the first element that satisfies the condition, while findIndex returns the index of that element
findIndex:
const data = [
{ id: 1, title: "first" },
{ id: 2, title: "second" },
{ id: 3, title: "third" },
{ id: 4, title: "fourth" },
];
const itemIndex = data.findIndex((element) => element.id === 3);
console.log(itemIndex);
Ouput:
2
find:
const data = [
{ id: 1, title: "first" },
{ id: 2, title: "second" },
{ id: 3, title: "third" },
{ id: 4, title: "fourth" },
];
const item = data.find((element) => element.id === 3);
console.log(item);
Output:
{ id: 3, title: 'third' }
destructuring
The destructuring assignment is a special syntax which enables unpacking (assign) value from arrays or object properties directly into variables
const name = ["jack", "pritom"];
const [firstName, lastName] = name;
console.log(firstName, lastName);
Output:
jack pritom
const data = {
id: 1,
name: "jack pritom",
loveMusic: true,
species: "human",
};
const { name: dataName, species, ...rest } = data;
console.log(dataName);
console.group(species);
console.group(rest);
Output:
jack pritom
human
{ id: 1, loveMusic: true }
rest & spread operator
Rest parameter enables us to pass unspecified number of parameters to a function which will be placed into array, while the spread operator enables us to spread the content of a iterable(i.e. array) into individual elements
Spread:
const introduction = ["my", "name", "is", "jack"];
const copyArr = [...introduction];
console.log(copyArr);
console.log(...copyArr);
Output:
[ 'my', 'name', 'is', 'jack' ]
my name is jack
Rest:
const getSize = (...args) => {
return args.length;
};
console.log(getSize(1, 2, 3));
console.log(getSize(10, 20, 30, 100));
Output:
3
4
promises
In simple terms promises are used to handle asynchronous operations. Each promise can end as a success or failure having 3 possible statuses: pending, fulfilled or rejected. In the example below we handle promises with async await syntax while fetching data from API
const fetchData = async () => {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/todos/");
if (!response.ok) throw new Error(response.status);
const result = await response.json();
return result;
} catch (e) {
console.log(e);
}
};
Top comments (32)
I would strongly suggest building a website without using a framework/library first, so you can understand what problem they are actually solving and gain an understanding of what goes on 'under the hood'. I've interviewed wayyyyyy too many developers who can throw something together in React, but are totally clueless when I ask them to do some basic stuff in vanilla JS. This problem is steadily getting worse and worse.
I have to respectfully disagree with this recommendation.
I concur itβs valuable for devs to understand the basics of JS, but these frameworks exist specifically to accelerate development. Unless Iβm hiring engineers to build a framework, I donβt need them to be experts in all the nuances that come with interfacing directly with browsers.
Thanks for the valuable suggestion !
I agree. Building an application with plain HTML, CSS and adding some interactivity with JS should be part of any curriculum before learning React. I would also throw in the usage of a template engine to fully appreciate the power of React.
Thanks for this. You have exposed an area I can exploit as a noob dev.
hi, nice post, but you could highlight js code more specifically to get colorful examples (read the editor doc).
It's always the same problem. Many aspiring devs think they know the language while they don't. They may use progress bars to describe their progression, but you can't master "80% of js" if you don't know how to achieve very common programs without using a framework and tons of add-ons.
The list you provided is great, and these are only basics of js.
Thank you !
@theaccordance I have to respectfully rebuff you here. Your statements are incorrect.
So is repeatedly calling a library a framework a tactic you use to weed out the fakers and posers? I mean, as an expert, you know we're talking about a library right? I think someone who understands computing enough to claim expertise should know the difference between these fundamental concepts.
I'm very curious to see how your way of doing things would be able to define a select element with values, handling a change event that mutates global document state in fewer lines than these:
This the point @natescode is making: It's impossible to implement the above in React without injecting hundreds of lines of boiler plate into your project and a layer of complexity that is mostly not required.
Even if we discount the technical debt maintaining your boilerplate code adds to your workload, this is your codebase:
Only the red outlined box in the middle represents actual project code... And even then, only a sliver on the far right hand side of the box... Most of
app
is polyfills because you used some CRA template and nobody knows how to Babel properly... If you had access to an expert instead of a bunch of React monkeys, maybe you could fix it...Point is, that if you were paid based upon how many lines of code you have to maintain, then you and your React monkeys would be making it rain compared to those silly novices who work with plain old JS.
I agree you have valid points, but I see one major flaw in your rebuttal:
We're clearly not building apps at the same scale here. You view all the overhead as a burden, whereas I have tangible savings in labor and code we maintain.
Yeah - your simple example proves your point, but my enterprise applications? They tell a different story.
Thanks. Um, destructuring, please; not destructing π€£.
ππ I have corrected it ,,,,,thanks for mentioning,,,
The point is correct, it is very important to strengthen the basics before learning anything, and these frameworks can't be learned in 2 days, we need to practice for many days, with that we need to make the basics strong, then we can do well. Thanks for the comment
Awesome for begginers
Thank you !
Greatt Post, its help and understant the overview thank you jack
This was very helpful, thanks!
Awesome! A lot of these are used a lot in React so knowing these beforehand will definitely improves the experience of a beginner reading some React code without getting overwhelmed
Thank you !
I agree that it is important to understand basic array methods before jumping into React. Unless React is required in school or on the job I suggest skipping it altogether because in my opinion React is way too much overkill. Plain Vanilla JS is the way to go for simple apps. Nice to understand some of the React basics though.
I wrote a Dev.to piece on 17 reasons why I prefer Plain JS over React. U can read it here at
dev.to/rickdelpo1/react-vs-plain-j...