JavaScript Functions and Strict Mode

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of using 'use strict'; in the script?

  • To improve code readability
  • To enable ECMAScript 6 features
  • To catch common coding errors (correct)
  • To increase performance

What does the pop method do to an array?

  • Removes the last element from the array (correct)
  • Removes an element from the beginning of the array
  • Returns the last element without removing it
  • Adds an element to the beginning of the array

What is the correct result of data.unshift(0) given const data = [1, 2, 3];?

  • [1, 0, 2, 3]
  • [0, 1, 2, 3] (correct)
  • [1, 2, 3, 0]
  • [2, 3, 0]

Which of the following statements about function expressions is true?

<p>They can be stored in variables. (A)</p> Signup and view all the answers

Which of the following correctly retrieves the last name of Moge?

<p>introduction.lastName (B)</p> Signup and view all the answers

What will the console.log statement for introduction.firstName return?

<p>Moge (B)</p> Signup and view all the answers

What does indexOf do in the context of array methods?

<p>Finds the index of a specific element in an array. (B)</p> Signup and view all the answers

Which statement accurately describes how arrow functions differ from regular function declarations?

<p>Arrow functions do not have their own 'this' context. (B)</p> Signup and view all the answers

What does the 'unshift' method do when applied to an array?

<p>It adds one or more elements to the beginning of the array. (B)</p> Signup and view all the answers

In the context of JavaScript, what is a primary characteristic of an object?

<p>Objects are collections of properties and methods. (B)</p> Signup and view all the answers

How does strict mode enhance JavaScript programming practices?

<p>It disables certain features to prevent potential errors. (B), It requires all variables to be declared before use. (D)</p> Signup and view all the answers

What is a key use case for nested functions in JavaScript?

<p>To enable one function to call another function directly. (B)</p> Signup and view all the answers

What is true about function expressions as compared to function declarations?

<p>Function expressions must be assigned to a variable. (B)</p> Signup and view all the answers

When using literal notation to create an array, which of the following is correct?

<p>Arrays can include different types of data. (B)</p> Signup and view all the answers

In JavaScript, what does the 'push' method do to an array?

<p>It adds one or more elements to the end of the array. (B)</p> Signup and view all the answers

What is a typical use case for Node.js in JavaScript development?

<p>Performing I/O operations and backend service handling. (A)</p> Signup and view all the answers

What will the return value of the method introduction.calcAge(2023) be?

<p>Moge is 25 years old (A)</p> Signup and view all the answers

What is the data type of the friends property in the introduction object?

<p>Array (D)</p> Signup and view all the answers

What will be the output of console.log(introduction.calcAge(2024))?

<p>Moge is 24 years old (C)</p> Signup and view all the answers

Which method is used to compute Moge's age in the introduction object?

<p>calcAge (B)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

JavaScript Strict Mode

  • Strict mode improves security and error detection in JavaScript.
  • Declared using 'use strict'; at the beginning of scripts.

Functions in JavaScript

  • Function Declaration: Uses the function keyword. Example:
    • function calcTwoNumbers(firstNumber, secondNumber)
  • Returns the product of two numbers.
  • Function Expression: Expressions can create functions, assigned to variables.
    • Example: const mySecondFunction = function(firstNumber, secondNumber) { return firstNumber * secondNumber; }
  • Arrow Functions: A shorter syntax for functions.
    • Example: const anArrowFunction = (a, b) => a * b;

Functions Calling Other Functions

  • Nested Functions: Functions can call other functions.
    • Example: cutFruit function cuts a fruit into pieces, then used in fruitProcessor to process apples and oranges.
  • Returns a string summarizing the operation.

Arrays in JavaScript

  • Creation: Using literal notation or the Array constructor.
    • Example using literal: const data = [1, 2, 3];
    • Example using constructor: const dataTwo = new Array('a', 'b', 'c');
  • Array Methods:
    • push: Adds an element to the end. Example: data.push(4)
    • pop: Removes the last element. Example: data.pop()
    • unshift: Adds an element to the start. Example: data.unshift(0)
    • shift: Removes the first element. Example: data.shift()
  • Array Manipulation: Includes checking for elements with indexOf and includes.

Objects in JavaScript

  • Objects are collections of properties and methods.
  • Example: const introduction contains personal information with properties like first name, last name, and birth year.
  • Methods: Functions defined within an object that operate on its properties.
    • Example: calcAge calculates age based on the current year.

Logging and Retrieving Properties

  • Accessing object properties directly using dot notation.
    • Example: introduction.firstName
  • Retrieving multiple properties can be done using array syntax.
    • Example: console.log([introduction.firstName, introduction.lastName]);

Node.js Context

  • Node.js allows server-side JavaScript execution, handling I/O operations, and providing backend services.
  • The concepts presented are foundational for both frontend and backend JavaScript development, emphasizing function and array manipulation.

JavaScript Strict Mode

  • Strict mode enhances security and error detection within JavaScript code.
  • Activated by adding 'use strict'; at the beginning of scripts or functions.

Functions in JavaScript

  • Function Declaration: Uses the function keyword to define functions.
    • Example: function calcTwoNumbers(firstNumber, secondNumber) returns the product of two numbers.
  • Function Expression: Functions can be defined within expressions and assigned to variables.
    • Example: const mySecondFunction = function(firstNumber, secondNumber) { return firstNumber * secondNumber; }
  • Arrow Functions: Provide a concise syntax for defining functions.
    • Example: const anArrowFunction = (a, b) => a * b;

Functions Calling Other Functions

  • Functions can call other functions, leading to nested structures.
    • Example: The cutFruit function can be used in fruitProcessor to process apples and oranges, returning a summary string.

Arrays in JavaScript

  • Creation: Can be created using literal notation or the Array constructor.
    • Literal: const data = [1, 2, 3];
    • Constructor: const dataTwo = new Array('a', 'b', 'c');

Array Methods

  • push(): Adds an element to the end of the array.
    • Example: data.push(4);
  • pop(): Removes the last element from the array.
    • Example: data.pop();
  • unshift(): Adds an element to the start of the array.
    • Example: data.unshift(0);
  • shift(): Removes the first element from the array.
    • Example: data.shift();
  • Array manipulation also includes methods like indexOf and includes for checking the existence of elements.

Objects in JavaScript

  • Objects are collections of properties and methods, encapsulating related data and functions.
    • Example: const introduction contains properties such as first name, last name, and birth year.
  • Methods: Functions defined within an object that operate on its properties.
    • Example: calcAge calculates age using the current year.

Logging and Retrieving Properties

  • Access object properties using dot notation.
    • Example: introduction.firstName retrieves the first name.
  • Multiple properties can be retrieved with array syntax.
    • Example: console.log([introduction.firstName, introduction.lastName]);

Node.js Context

  • Node.js enables server-side JavaScript execution, facilitating I/O operations and backend services.
  • Understanding these JavaScript concepts is essential for both frontend and backend development, especially in function and array manipulation.

Object Structure

  • Represents an object named introduction.
  • Contains various key-value pairs that describe a person.

Properties

  • firstName: Assigned the value 'Moge'.
  • lastName: Assigned the value 'Okulaja'.
  • birthYear: Set to 1999, indicating the year of birth.
  • job: Defined as 'Software Engineer', indicating professional occupation.
  • friends: Array containing names ['Ella', 'Lilian', 'Asma', 'Madiss'], showcasing personal relationships.

Object Methods

  • calcAge: A function that calculates age based on the current year provided as an argument.
    • Uses currentYear to subtract birthYear for age calculation.
    • Returns a string: "Moge is X years old", where X is the calculated age.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser