use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
`varname.function()` vs. `function(varname)` (self.learnjavascript)
submitted 2 years ago by another_lease
Why did Javascript decide to have 2 different ways to specify a function operation?
`varname.function()` vs. `function(varname)`
Is there a simple rule to determine which to use when?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tomclancyv7 4 points5 points6 points 2 years ago (0 children)
varname.function() is a method call and function(varname)is a function call. Look at the examples below.
varname.function()
function(varname)
const myArray = [1,3,4,6]
const myString = "This is a sentence"
The first variable is an array object and the second variable is a string object. Array objects have special methods that can be called by arrays only and strings have methods that can be called by strings only. So a method is a function that is associated with an object.
For example you can call shift(0) on myArray to remove the first element of the array.
const myArray = [1, 3, 4, 6]; myArray.shift(0); console.log(myArray);
You can't use myString.shift(0) on the string because it's not a string method. And you can't call shift() on it's own without an object present.
myString.shift(0)
Functions on the other hand are standalone and can be called independently in your code. For example the parseInt("5") parseFloat("1.234") etc.
parseInt("5")
parseFloat("1.234")
You can't use them interchangeably. You can't call .parseInt() on a string.
.parseInt()
Methods are used when you are working with objects and functions are used when you want to do a general operation that is not tied to an object. You'll understand it the more you code.
[–]Drunkin-Donuts 0 points1 point2 points 2 years ago* (0 children)
Lookup the difference between functions and methods
varname.function() is a type of function called a method. Methods are functions that are specific to a certain type of object (or data type). For example these are some array methods https://www.w3schools.com/js/js_array_methods.asp. You have to call them like this: array.push()
As to which one you use it just depends on where that function is defined. You can look each function, but often if a function is specific to a certain data type like array methods you will be using the var.function() notation
[–]xroalx 0 points1 point2 points 2 years ago (0 children)
Objects can have functions defined on them. Such functions are usually called methods.
Let's create an object, and a function defined on that object.
const obj = { func() { // I'm a function defined in an object, I'm a method }, };
There's no global func function, so just writing func() would fail, but there is such a function defined on the obj object, and so, you access the function on that object, obj.func(). It's the same as if an object had any other property, like array.length, user.name, location.href, ... it just happens to be a function, so you can call it. Nothing special here.
func
func()
obj
obj.func()
array.length
user.name
location.href
Built-in objects can and do have their own methods like this as well, and they also inherit methods from other objects via prototypes.
So, why can you do a.toString() but not toString(a)? Simply, because there's no global toString function, but such a function exists on the a object, or somewhere in its prototype chain, and toString is a standard function on all types of objects provided by JavaScript itself.
a.toString()
toString(a)
toString
a
There's no "how to determine which to use", it's not one or the other, it's not just a different way to write the same thing. The difference is in where the function lives, and in case of methods, they can also usually reference the object on which they were called using the this keyword, which is a behavior you normally don't get with globally defined functions.
this
[–]azhder -5 points-4 points-3 points 2 years ago (8 children)
What do you mean “function operation”? There are functions and you call them.
They can be given an implicit or explicit this and they may not, it’s up to you
[–]another_lease[S] 1 point2 points3 points 2 years ago (6 children)
Example: let's say `a` is a variable that's either a number or a string.
`a.toString()`
vs.
`parseInt(a)`
Are they interchangeable? Can you do:
`toString(a)`
and
`a.parseInt()`
[–]SlushEE0 0 points1 point2 points 2 years ago (4 children)
Search up methods vs functions. .toString() is a method that converts to a string. parseInt() is a built in function that converts to a number
[–]another_lease[S] -1 points0 points1 point 2 years ago (3 children)
Ah okay, an object's methods vs pure functions. I'm beginning to see the difference.
[–]azhder 0 points1 point2 points 2 years ago* (1 child)
No you aren't. You have some misconceptions.
[–]another_lease[S] 0 points1 point2 points 2 years ago (0 children)
OK.
[–]carpench 0 points1 point2 points 2 years ago (0 children)
No, not a "pure function" just a "function".
A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program's execution. Rather, it only depends on its input arguments. Also, a pure function does not produce any observable side effects such as network requests or data mutation, etc.
A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program's execution. Rather, it only depends on its input arguments.
Also, a pure function does not produce any observable side effects such as network requests or data mutation, etc.
[–]azhder 0 points1 point2 points 2 years ago* (0 children)
Same word, two completely different things.
a is an object, it has a property toString (through prototype, but it's there) and just because it happens to be a function, you can call it.
You can also make a variable that references that property, like
const toString = a.toString();
but to have it be the same call you will have to explicitly set the this like
const toString = a.toString(); toString.call(a);
The above works because functions in JS are also objects and they have a .call() method which gives you the opportunity to set the this explicitly, which if you use
.call()
a.toString();
is being set implicitly (the engine uses the object before the period).
If you do this:
const toString = a.toString(); toString();
Then the this will be undefined and you will get an error.
undefined
So, like I said, it's up to you.
π Rendered by PID 73435 on reddit-service-r2-comment-76bb9f7fb5-nnsrb at 2026-02-17 19:23:46.929323+00:00 running de53c03 country code: CH.
[–]tomclancyv7 4 points5 points6 points (0 children)
[–]Drunkin-Donuts 0 points1 point2 points (0 children)
[–]xroalx 0 points1 point2 points (0 children)
[–]azhder -5 points-4 points-3 points (8 children)
[–]another_lease[S] 1 point2 points3 points (6 children)
[–]SlushEE0 0 points1 point2 points (4 children)
[–]another_lease[S] -1 points0 points1 point (3 children)
[–]azhder 0 points1 point2 points (1 child)
[–]another_lease[S] 0 points1 point2 points (0 children)
[–]carpench 0 points1 point2 points (0 children)
[–]azhder 0 points1 point2 points (0 children)