How to perform computation inside expression using ternary operator?


Expressions can be used for any computation, below example uses the ternary operator to determine whether length and breadth are equal or not. Based on that identify it is square or rectangle and also calculate area of that.

Example

import React, { Component } from 'react';
 
const length = 10, breadth = 20;
 
class App extends Component {
  render = () => 
  <span class="d-block p-2 bg-dark text-white">
    Area of { length == breadth ? "Sqaure" : "Rectangle" }: { length * breadth}
  </span>
}
 
export default App;
Most Helpful This Week