Functions in Programming

FlashyCouplet avatar
FlashyCouplet
·
·
Download

Start Quiz

Study Flashcards

16 Questions

What is the purpose of a function?

To execute a block of code multiple times from different parts of a program

What is the difference between a function declaration and a function expression?

A function declaration is defined with a name, while a function expression is anonymous

What is the purpose of the push() method in an array?

To add one or more values to the end of the array

What is the difference between indexOf() and includes() methods in an array?

indexOf() returns the index of the value, while includes() returns a boolean

What is the purpose of the map() method in an array?

To create a new array by applying a function to each value in an existing array

What is the scope of a variable defined inside a function?

Local scope

What is the purpose of the splice() method in an array?

To add or remove values from a specific position in the array

What is the purpose of hoisting in functions?

To allow functions to be called before they are defined

What is the default scope of a variable declared outside a function or block scope?

Global

Which of the following is a characteristic of variables declared with let and const?

They are not hoisted to the top of their scope

What is the purpose of the prototype chain in object-oriented programming?

To inherit properties from a parent object

What is the primary difference between a function declaration and a function expression?

The way they are declared

What is a closure in the context of functions?

A function that has access to its own scope and the scope of its parent functions

Which of the following is an example of a primitive type in JavaScript?

String

What is the purpose of a constructor in object-oriented programming?

To initialize an object with default values

Which of the following is an example of a reference type in JavaScript?

Object

Study Notes

Functions

  • A block of code that can be executed multiple times from different parts of a program
  • Can take arguments, which are values passed to the function when it's called
  • Can return values, which can be used by the code that called the function
  • Can be defined with a name, or anonymously
  • Types of functions:
    • Function declaration: function name(parameters) { code }
    • Function expression: const name = function(parameters) { code }
    • Arrow function: const name = (parameters) => { code }
  • Function scope: variables defined inside a function are only accessible within that function
  • Hoisting: function declarations are moved to the top of their scope, allowing them to be called before they're defined

Arrays

  • A data structure that stores a collection of values
  • Values can be of any type, including strings, numbers, and objects
  • Indexed, meaning each value has a unique index or key
  • Can be created using the [] syntax or the Array constructor
  • Methods:
    • push(): adds one or more values to the end of the array
    • pop(): removes the last value from the array and returns it
    • shift(): removes the first value from the array and returns it
    • unshift(): adds one or more values to the beginning of the array
    • splice(): adds or removes values from a specific position in the array
    • indexOf(): returns the index of the first occurrence of a value in the array
    • includes(): returns a boolean indicating whether a value is in the array
  • Array manipulation:
    • Concatenation: combining two or more arrays using the spread operator (...)
    • Slicing: creating a new array from a subset of an existing array using the slice() method
    • Mapping: creating a new array by applying a function to each value in an existing array using the map() method
    • Filtering: creating a new array by selecting values from an existing array that meet a certain condition using the filter() method
    • Reducing: reducing an array to a single value by applying a function to each value using the reduce() method

Functions

  • A block of code that can be executed multiple times from different parts of a program, allowing for code reuse and modularity.
  • Functions can take arguments, which are values passed to the function when it's called, and use them to perform calculations or operations.
  • Functions can also return values, which can be used by the code that called the function, allowing for more complex logic and data flow.
  • Functions can be defined with a name, or anonymously, and can be assigned to variables or passed as arguments to other functions.
  • There are three types of function definitions: function declaration, function expression, and arrow function, each with its own syntax and use cases.

Arrays

  • A data structure that stores a collection of values, which can be of any type, including strings, numbers, and objects.
  • Arrays are indexed, meaning each value has a unique index or key, allowing for efficient access and manipulation.
  • Arrays can be created using the [] syntax or the Array constructor, and can be manipulated using various methods.
  • Array methods include:
    • push(): adds one or more values to the end of the array.
    • pop(): removes the last value from the array and returns it.
    • shift(): removes the first value from the array and returns it.
    • unshift(): adds one or more values to the beginning of the array.
    • splice(): adds or removes values from a specific position in the array.
    • indexOf(): returns the index of the first occurrence of a value in the array.
    • includes(): returns a boolean indicating whether a value is in the array.
  • Arrays can be manipulated using various techniques, including:
    • Concatenation: combining two or more arrays using the spread operator (...).
    • Slicing: creating a new array from a subset of an existing array using the slice() method.
    • Mapping: creating a new array by applying a function to each value in an existing array using the map() method.
    • Filtering: creating a new array by selecting values from an existing array that meet a certain condition using the filter() method.
    • Reducing: reducing an array to a single value by applying a function to each value using the reduce() method.

Variables

  • Variables are declared using the let, const, and var keywords.
  • JavaScript has two main types of variables: Primitive and Reference.
  • Primitive types include number, string, boolean, null, undefined, and symbol.
  • Reference types include object, array, and function.
  • Variables have a scope, which determines their accessibility.
  • Global variables are declared outside a function or block scope.
  • Local variables are declared inside a function or block scope.
  • Variables declared with var are "hoisted" to the top of their scope.
  • Variables declared with let and const are not "hoisted".

Functions

  • Functions are declared using the function keyword.
  • There are two main types of functions: Function Declaration and Function Expression.
  • Function Declaration is a function declared as a separate statement.
  • Function Expression is a function declared as an expression, often assigned to a variable.
  • Functions can take arguments, which are passed when the function is called.
  • Functions can return a value using the return statement.
  • A function has access to its own scope and the scope of its parent functions, creating a closure.
  • Arrow Functions are a concise way to declare functions, introduced in ECMAScript 6 (ES6).

Object-Oriented Programming (OOP)

  • Objects are collections of key-value pairs, created using the {} syntax.
  • Properties are the key-value pairs within an object.
  • Methods are functions within an object, used to perform actions on the object.
  • Inheritance is the process of creating a new object based on an existing object, using the prototype chain.
  • Constructors are special functions used to create new objects, often with the new keyword.
  • Prototypes are objects that serve as a blueprint for other objects, allowing for inheritance and method sharing.
  • The this keyword refers to the current object being executed, often used in methods and constructors.

Understand the concept of functions in programming, including declarations, expressions, and arrow functions. Learn about function arguments and return values.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser