7 Essential JavaScript syntax you should know before learning React
Tags: webdev, javascript, react, frontend
Published: 2024-04-06 07:23:52

The roadmap to becoming a seasoned front-end developer is quite technical and time-consuming. This roadmap is a sequential step of languages that should be mastered and honed, equipping developers with the necessary skills to thrive in front-end web development. In this roadmap, the prior language could serve as a building block for the next, and as such not learning these languages in the right order might prove difficult going forward.

JavaScript is the programming language of the web and it serves as the backbone of modern web development. JavaScript programs the behavior of the web enabling dynamic and interactive user experiences. Its syntax forms the basis upon which frameworks and libraries, such as React, and Vue are built. For developers aspiring to dive into the world of React development, mastering JavaScript is not merely advisable, it’s indispensable.
In this article, we’ll delve into seven essential JavaScript syntax concepts that are essential for effective React development. Understanding these fundamental building blocks equips developers with the tools needed to harness the full potential of React, enabling them to design sophisticated user interfaces with ease and efficiency. So, if you are just embarking on your React journey or seeking to deepen your understanding of the indispensable importance of JavaScript in front-end development, join us as we explore some of the various JavaScript syntaxes used in React.


### 1. Function Declaration
  A JavaScript function is a block of code designed to perform a particular task. A JavaScript function will only be executed when it is called i.e. invoked. The function keyword defines a JavaScript function, followed by a name and then parentheses (). The parentheses may include parameters separated by commas and the code to be executed is placed in curly brackets{}.
```
function name(parameterA, parameterB) {
  // code to be executed
}
```
In React, components serve as the building blocks of user interfaces. These components in simpler terms are a fraction of the UI elements such as the header, nav bar, footer, etc. React components are conventionally declared as JavaScript functions that return JSX (JavaScript XML) elements, a syntax extension for JavaScript that allows writing HTML-like code within JavaScript. Employing components simplifies the rather cumbersome process of building complex user interfaces.
For example, a simple functional component in React might look like this
```c
import React from 'react'

function MyComponent () {
  return(
    <div>
      <h1> Hello, World!</h1>
      <p> This is a functional component in React </p>
    </div>
);
}
export default MyComponent;
```

In this example, when the component is rendered in the application, the MyComponent function returns a div with an h1 heading and paragraph.

### 2. Array Destructuring

In JavaScript, array destructuring is a powerful feature that allows developers to extract multiple values from arrays and assign variables to them.
```
Const numbers =[1, 2, 3, 4, 5]; //an array of numbers

Const [first, second, third] = numbers; //the numbers array destructured, extracting values and assigning them to variables.
```
Each variable is assigned the value of the corresponding element in the array.
Similarly, this technique is used to unpack values from arrays returned by hooks. 
For example, the UseState hook that manages states in a React component.
```c
import React, {useState} from ‘react’;
Const [state, setState] = useState()
```
In the code snippet above, the useState hook by default returns an array with two elements, the current state value i.e. state, and a function (setState) to update the state. By using array destructuring, extracting these elements and assigning them to variables (state and setState) is easy.

### 3. Object Destructuring

This is similar to array destructuring, the only difference here is that this extracts properties from objects instead of an array and assigns them to variables.

```c
import React from 'react';

function UserProfile () {
  const user ={
    name: 'Joy Meyers',
    age: 24,
    city: 'Lisbon'
};

//object destructuring to extract objects
const {name, age, city } = user;

return(
  <div>
    <h2> User Profile </h2>
    <p> Name: {name} </p>
    <p> Age: {age} </p>
    <p> City: {city} </p>
</div>
);
}

export default userProfile;
```
Object destructuring allowed us to extract the name, age, and city properties and directly access them within the component.

### 4. Mapping

In JavaScript, the map() method produces a new array by iterating on every element in an array and performing a function on them. 
```
const surname = [Harry, Jude, Jadon, Luke]
const name = surname.map(myFunction()) 

function myFunction(value) {
  return value;
}
```

```c

import React from 'react';

function LanguageList(){
  const languages = ['English', 'French', 'Spanish', 'Italian', 'Latin']

  const language = languages.map((language)  => <li>{language}</li>)

  return (
    <div>
      <h2>Language List</h2>
      <ul>{language}</ul>
    </div>
  )
}

export default LanguageList;
```

In the example above, we see how the map method is used on an array to return each array item as an unordered list.

### 5. Conditional Statement
The if statement is used to specify that a block of JavaScript code should be executed if a condition is true. The else statement is used to specify that a block of Javascript code should be executed if the condition is false while the else if statement is used to specify a new condition if the first one is false.
```c
if (conditionA) {
  //  block of code to be executed if conditionA is true
} else if (conditionB) {
  //  block of code to be executed if the conditionA is false and conditionB is true
} else {
  //  block of code to be executed if the conditionA is false and conditionB is false
}
```
In React, a combination of any of these three statements is used in conditional rendering when a variable will determine the property of an element.

### 6. Ternary expressions
In JavaScript, a ternary expression is a concise way to write conditional statements. It consists of three parts: a condition, a value to return if the condition is true, and a value to return if it is false.
```
Condition ? expression1 : expression2
```
It is generally considered a best practice in React to use ternary expressions or logical&& operators for conditional rendering instead of traditional if statements.
```c
function conditionalRendering(){
  const isConnected = true
  return(
    <>
    <h2>conditional Rendering</h2>
    {isConnected ? <p>Welcome back!</p>: <p> Check your connection</p>} 
    </>
  )
}
export default conditionalRendering;
```

### 7. Logical && operator
In JavaScript, conditional rendering using logical && operators will only render the expression on the right hand side if the expression on the left hand side is true.
```
Expression1 && <p> Conditional Rendering </p>
```
If the expression on the left is false, the entire expression short-circuits, and the expression on the right is not evaluated.
```c
function conditionalRendering(){
  const isConnected = true
  return(
    <>
    <h2>conditional Rendering</h2>
    {isConnected && <p>Welcome back!</p>} 
    </>
  )
}
export default conditionalRendering;
```


## Conclusion
In conclusion, understanding these JavaScript concepts is crucial for effective React Development. In this article, we covered seven fundamental JavaScript syntaxes which should be at the tip of your fingers if you aspire to be an effective React developer.
Getting familiar with these JavaScript concepts allows developers to write cleaner and concise codes, and ultimately build more complex and efficient React applications.