Switches and routers are both networking devices that operate at different layers of the OSI model and perform different functions. The key differences between L2 switches and routers are:
Switches operate at the data link layer (Layer 2) of the OSI model, while routers operate at the network layer (Layer 3). This means that switches forward frames based on the MAC addresses of the source and destination devices, while routers route packets based on the IP addresses of the source and destination networks.
Switches build a MAC table that maps MAC addresses to switch ports, while routers build a routing table that maps IP addresses to next-hop interfaces. The MAC table is used by switches to determine which port to send a frame to, while the routing table is used by routers to determine which interface to send a packet to.
Switches offer a considerably higher port density than routers, meaning that they can connect more devices to the network. Switches typically have dozens or hundreds of Ethernet ports, while routers usually have a few Ethernet ports and other types of ports such as ADSL, cable, fiber, dial-up, etc. Switches are used to create LAN segments, while routers are used to connect different networks or subnets.
Switches are faster than routers because they do not take up time looking at the network layer header information. Switches can forward frames at wire speed, while routers need to process packets at the network layer and perform additional functions such as NAT, firewall, QoS, etc.
References: Switch (L2/L3) Vs Router: Comparison and Differences in TCP/IP Networks, Key Differences Between Routers and Different Types of Switches, Layer 2 Switch - How it operates, when to use it - Network Encyclopedia, Layer 2 switching - Study-CCNA, Differences Between Layer 2 and Layer 3 Switches|Which one do you need?