The short answer is yes. You can lose or gain weight eating anything you want in the appropriate quantity. This has been demonstrated in numerous PR stunts where people have got ripped eating nothing but fast food or lost significant amounts of weight eating nothing but cookies.
That does not mean it is a good idea.
For a start food is more than just a caloric number. It is also a source of nutrition. There is a difference between eating 100 calories of refined sugar and eating 100 calories of fresh veggies.
Your body needs all the micronutrients found in real food to thrive.
Furthermore reducing calories is so much easier, most sustainable and healthier when you derive the bulk of those calories from real food.
Why?
Hunger is the enemy of weight loss. It gnaws away at your resistance to snacks, junk food, large portions and second helpings. Whilst you may win some battles, hunger normally wins the war.
Overcoming the urge to constantly eat is therefore an important part of successfully creating a calorie deficit and losing weight. No hunger, no problem! With higher levels of protein, fibre and water real foods allow you to eat larger portions without the burden of excessive calories.
Compare 1500 calories of pizza to 1500 calories of lean meat and veggies and you will quickly see what I mean.
Health benefits aside the latter will keep you fuller for longer.
Last but not least modern food processing increases the calorie availability of food which means a greater percentage of junk food calories end up in your body. It is like a form of pre-digestion breaking down complex foods into easily-absorbed energy. Sugar, refined carbs and oils (the staple ingredients in most junk foods) are the primary culprits.
On the flip side the calories found in real foods are less readily available and require more work from your digestive system to access. This results in a further reduction in net-calorie intake.
So yes, you can eat junk foods and still lose weight. But the more you do the harder it becomes!