The switching frequency at which the inverter operates is an important parameter that significantly affects the operation of the inverter. In this paper, a new variable switching frequency algorithm is proposed in which the switching frequency is varied in response to the variation in the operating conditions. The proposed algorithm is based on multi-objective optimization problem formulation such that a weighted sum of the switching losses in the inverter and the Total Demand Distortion (TDD) in the inverter's output current is to be minimized. A behavioral model for the switching devices and a TDD model based on the time-domain ripple analysis are adopted. This made the formulation of the problem to be very clear and easy to be implemented in the digital controller. Therefore, the optimal switching frequency can be determined online. The proposed algorithm can increase the efficiency of the inverter without deteriorating the harmonic performance. Moreover, the lifetime of the inverter can be increased due to the narrower junction temperature profile that can be achieved by this algorithm when compared to the fixed switching frequency counterpart. The effect of the ambient temperature is taken into account and the switching frequency is varied accordingly. The algorithm is implemented on the Field-Programmable Gate Array (FPGA) platform. The effectiveness of the proposed algorithm has been verified experimentally.