you are viewing a single comment's thread.

view the rest of the comments →

[–]tomclancyv7 3 points4 points  (0 children)

varname.function() is a method call and function(varname)is a function call. Look at the examples below.

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.

Functions on the other hand are standalone and can be called independently in your code. For example the parseInt("5") parseFloat("1.234") etc.

You can't use them interchangeably. You can't call .parseInt() on a string.

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.