r/javascript • • Aug 23 '17

10 JavaScript concepts you need to know for interviews

https://dev.to/arnavaggarwal/10-javascript-concepts-you-need-to-know-for-interviews
425 Upvotes

118 comments sorted by

View all comments

Show parent comments

2

u/SgtBlackScorp Aug 24 '17

I'm actually interested in what exactly the difference between *val and &val is. Could you elaborate?

2

u/BB611 Aug 24 '17

*val - a pointer (memory address)
&val - a reference (also a memory address)

For most purposes, these two are the same.

However, if you have a function that takes a pointer like testFunc(int *val), every time you pass a pointer to it you make a copy of the pointer. You can change the passed pointer without changing the original.

A function that takes a reference, like testFunc(int &val), doesn't make a copy of the value, so if you change that reference you will also change the original reference.

There are also some fundamental differences in the way C++ treats pointers and references, but I assume you aren't that interested in them for the purpose of this question.

1

u/SgtBlackScorp Aug 24 '17

So a pointer is a memory address where the memory address of the reference is saved?

1

u/BB611 Aug 24 '17

No, a pointer is a memory address where you can find a value of the same type as the pointer (in this case, int).

A pointer to a pointer is a memory address where the memory address of the reference is saved.

It sounds like you're not familiar with C++, if you're interested in more I recommend something from the Beginner/Introductory lists here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

1

u/SgtBlackScorp Aug 24 '17

Apart from 2 or 3 lectures for c in college I have never done anything with c/c++, but thanks for explaining. I'll check out the beginner's guide when i have some time.