I don't think you have for:
loops in LWC, however if you use child components you can sort of do what you want based on index.
Step 1. Define a child component
js
import { LightningElement, track, api } from 'lwc';
export default class ChildComponent extends LightningElement {
@api index;
get shouldDisplay() {
return this.index <= 4;
}
}
html
<template>
<div if:true={shouldDisplay}>
whatever we want here
</div>
</template>
Step 2. Use child component in our loop in the parent component
<template for:each={contacts} for:item="val" for:index="idx">
<c-child-component index={idx></c-child-component>
</template>
(you can also pass along your actual object etc. any other data from val
obviously)
This will not stop the loop executing but the items with index > 4 won't render.