top of page
Logo der Online Agentur mdwp

Variable

A variable in React JS is a storage location that has a name, known as an identifier, for storing data values. The value stored in a variable can be changed during program execution. The variable is used to hold temporary data used in manipulation and computation processes.

In JavaScript/React, the variable is defined using three types of keywords:

1. "var" - Scope is global, or local if declared inside a function.
2. "let" - Scope is block scope (local to the code block)
3. "const" - Also a block scope, this variable can't be reassigned, but the content can be modified if it’s an object or an array.

Here is an example of how to define variables in React:

```jsx
import React from 'react';

class Example extends React.Component {
render() {

let greeting = 'Hello, World!';
const name = 'John';

return (
<div>
<h1>{greeting}</h1>
<h2>Welcome, {name}</h2>
</div>
);
}
}

export default Example;
```

In the snippet above, "greeting" and "name" are variables storing strings 'Hello, World!' and 'John' respectively. Later, these are embedded in JSX using {}.

bottom of page