Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1414

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T07:57:09+00:00 2024-11-25T07:57:09+00:00

All About JS Functions

  • 62k

Not All, I have tricked you.


typeof functions

When you do typeof <someFunction>, you get back 'function'. But, you might have heard that functions are objects??

image

Well, they are indeed objects, if you do console.dir() on a function, you will get an object with key value pairs and a prototype, the reason why you get 'function' when you pass it as an operand to typeof operator is because that is the way typeof is configured

image

As you can see in the image, objects which are callable return 'function'

function declarations vs function expressions

//function declaration  function doSth() {   //do sth }   // function expression  const doSth = ()=> {   // do sth } 
Enter fullscreen mode Exit fullscreen mode

what are the implications of using either one of those??

Functions and variables are hoisted (as if they are lifted up at the top of the code, metaphorically). But only the declaration is hoisted, this has a very important implications, variables will be declared but they will not be assigned a value (another term of assigning a value is initializating). That means if you call functions before they are declared, you can but if you try to access a variable before it is declared, you will get

image

BONUS for English Non-native Speakers: hoisting actually means to lift sth up, it is not a word that is javascript-specific.

The image below shows a hoist (lifter)

image

So, what are the implications again??

You can declare functions which are in the form of function declarations AFTER they are called but you can't with function expressions because they are variables and they are assigned a function (which is also a value or variable)

So you can do this

doSth()  function doSth() {   //do sth } 
Enter fullscreen mode Exit fullscreen mode

But not this

doSth()  const doSth = ()=> { //do sth }  
Enter fullscreen mode Exit fullscreen mode

What happens if I call a function that expects two arguments with one argument, will the function run??

Yes, it will run. The second argument will be undefined.

If I want to call a function but I dont know the number of arguments beforehand, what should I do?

There are two ways to do it:

ES5 way

function getSum() {   let sum = 0;   for (const number of arguments) {     sum += number;   }   return sum; }  console.log(getSum(1, 2, 3, 4));  
Enter fullscreen mode Exit fullscreen mode

WHAT IS “ARGUMENTS”, WHERE DID THAT ARRAY COME FROM??

That is actually an array-like object, not an array. Array-like objects are objects that have a length property and have indices as properties which have array-like items as values

image

Array-like objects are iterables in js and that means we can iterate over them using for..of or for i = 0 loops but not forEach because that is an array method.

arguments is a reserved keyword or variable and its value is an array-like object of all arguments you called the function with

ES6 way

With es6, we have a new operator called the rest operator.

function getSum(...numbers) {   let sum = 0;   for (const number of numbers) {     sum += number;   }   return sum; } console.log(getSum(1, 2, 3, 4));  
Enter fullscreen mode Exit fullscreen mode

The rest operator simply combines values passed into an array. The naming behind comes from that we can do this:

function getSum(firstNumber, ...numbers) {   let sum = 0;   for (const number of numbers) {     sum += number;   }   return sum; }  
Enter fullscreen mode Exit fullscreen mode

Now the first argument passed will be assigned to firstNumber and ''the rest'' of the arguments will form an array called numbers


That's the end of this article. Thanks for reading. I hope I have added something new to your arsenal of knowledge. If you liked the article, a quick thumps up or a comment would be appreciated and would help me continue sharing information. See you in the next one!

beginnersfrontendjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.