Investing in the stock market is one of the best ways to build wealth over the long term, but it can be daunting. Not only does it involve a certain amount of risk, but it can also be expensive to invest.

Many popular stocks cost hundreds or even thousands of dollars per share, and if money is already tight, investing that kind of cash simply isn't feasible for many people.

The good news, though, is that you don't have to have a huge pile of cash lying around to invest in the stock market. In fact, there are two ways you can get started investing with just a few dollars.

Young man holding hundred dollar bills in front of his face

Image source: Getty Images.

1. Index funds

Index funds are essentially large collections of stocks, bonds, or other securities that track a particular stock market index, such as the S&P 500 (SNPINDEX:^SPX) or the Dow Jones Industrial Average (DJINDICES:^DJI).

There are several advantages to investing in index funds. For one, you only invest what you can afford. Unlike investing in individual stocks, you don't have to buy shares of index funds. Rather, you can simply invest however much you choose, whether it's $5 or $5,000.

Another significant advantage of index funds is that they provide instant diversification. A single index fund may include dozens, hundreds, or even thousands of different stocks, and you're instantly investing in all of them when you invest in an index fund. This diversification means that if a few of those stocks don't perform well, it won't sink your entire portfolio.

2. Fractional shares

As their name implies, fractional shares are simply partial shares of a company's stock. So instead of buying a full share of stock for hundreds or even thousands of dollars, you can buy just a small sliver of the stock for as little as one dollar.

Fractional shares are a great option if you have your eye on a particular stock but can't afford to buy a full share right now. They also make it much easier to diversify your portfolio, because you can invest in many different stocks for less than the price of buying a full share of a single stock.

There are a few caveats to investing in fractional shares, though. For instance, different brokerages have different rules regarding which companies you can invest in and whether fractional investing is even an option. In addition, not all stocks are even available as fractional shares. So if there are particular stocks you have in mind, check with your brokerage to see whether fractional investing is offered, as well as whether the stocks you have your sights set on are available as fractional shares.

Which option is best for you?

Both index funds and fractional shares have their advantages, and both are solid options for people who want to invest in the stock market without spending an arm and a leg.

If you're looking for a more hands-off approach to investing, index funds might be the best choice. When you invest in an index fund, you don't need to decide which individual stocks to invest in -- you automatically invest in whatever stocks are in the index.

Index funds also follow the market, meaning they'll likely take a hit if the market as a whole crashes, but they'll also bounce back as the market recovers. For that reason, they're great "set it and forget it" types of investments because you don't need to worry about when is a good time to buy or sell. If you invest in an index fund now and leave it alone for a few decades, you'll likely see positive returns.

If you'd prefer a more customized investing option, you may opt for fractional shares. One of the downsides of index funds is you're stuck with the stocks in the index, whether you like them or not. With fractional shares, you can invest only in the companies you choose. That does require more research because you'll need to ensure you're investing in solid long-term companies, but it also allows you to be more hands-on.

Finally, there's no rule saying you can't invest in both. Maybe you build a core portfolio composed of index funds, for example, then use any spare cash to dabble in fractional shares. How you choose to invest will depend on your personal preferences, but both of these options can provide a more affordable way to invest.