JavaScript Introduction II
39 Questions
0 Views

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 a defining feature of variables declared with const?

  • They have block scope. (correct)
  • They can be reassigned later.
  • They can be redeclared multiple times.
  • They must have a global scope.

How can a property of a JavaScript object be accessed?

  • By calling objectName[propertyName] method.
  • Invoking objectName.propertyName() function.
  • Using the method objectName->propertyName.
  • Using objectName.propertyName or objectName['propertyName'] (correct)

What does the function toCelsius(fahrenheit) do?

  • Returns the input temperature as is.
  • Calculates the freezing point of water.
  • Converts Fahrenheit to Celsius. (correct)
  • Converts Celsius to Fahrenheit.

Which of the following statements about JavaScript objects is false?

<p>They are immutable in nature. (B)</p> Signup and view all the answers

What result does person.fullName() return?

<p>A concatenation of firstName and lastName. (A)</p> Signup and view all the answers

What is true regarding HTML events?

<p>They encompass both browser and user actions. (D)</p> Signup and view all the answers

Which method correctly calls an object method?

<p>objectName.methodName(); (B)</p> Signup and view all the answers

Which of the following is NOT a characteristic of const variables?

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

In JavaScript, an object allows for the grouping of which of the following?

<p>Related data (properties) and behavior (methods). (D)</p> Signup and view all the answers

To access the last name of a person stored in an object, which of the following syntaxes is correct?

<p>person.lastName; (C)</p> Signup and view all the answers

What does the slice() method do when a second parameter is omitted?

<p>Slices out the rest of the string (A)</p> Signup and view all the answers

What is the output of the code: let part = str.slice(-12); where str is 'Apple, Banana, Kiwi'?

<p>Banana, Kiwi (B)</p> Signup and view all the answers

How does substr() differ from slice() in terms of the second parameter?

<p>It specifies the length of the extracted part (D)</p> Signup and view all the answers

What will the following code output: let part = str.substr(-4); where str is 'Apple, Banana, Kiwi'?

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

What is a key characteristic of the replace() method?

<p>It only replaces the first match found (C)</p> Signup and view all the answers

If str is 'Apple, Banana, Kiwi', what will str.slice(7, 13) return?

<p>Banana (A)</p> Signup and view all the answers

What happens if the first parameter of slice() is negative?

<p>It starts counting from the end of the string (C)</p> Signup and view all the answers

What will be the output of the following code: let part = str.substr(7, 6); if str is 'Apple, Banana, Kiwi'?

<p>Banana (A)</p> Signup and view all the answers

What is the result of calling str.replace('a', 'o') on the string 'Apple, Banana, Kiwi'?

<p>Apple, Bonana, Kiwi (D)</p> Signup and view all the answers

What does the slice() method return if both parameters are negative, e.g., str.slice(-5, -1)?

<p>Kiwi (C)</p> Signup and view all the answers

What will be the output of the following code: let text = 'MSA students and MSA staff!'; let newText = text.replace('MSA', 'CS'); console.log(newText);?

<p>CS students and MSA staff! (C)</p> Signup and view all the answers

Which of the following statements correctly uses the concat() method?

<p>let fullName = text1.concat(' ', text2); (A)</p> Signup and view all the answers

What does the toUpperCase() method do?

<p>It converts all characters in a string to upper case. (C)</p> Signup and view all the answers

What is the purpose of the trim() method in string manipulation?

<p>It removes whitespace from both sides of a string. (C)</p> Signup and view all the answers

In which scenario would you use the split() method?

<p>To convert a string to an array based on a delimiter. (A)</p> Signup and view all the answers

How would you correctly call the toLowerCase() method on a string variable text1?

<p>text1.toLowerCase(); (A)</p> Signup and view all the answers

What does the output of the following code produce: let text1 = ' welcome!'; let text2 = text1.trim(); console.log(text2);?

<p>welcome! (A)</p> Signup and view all the answers

What is expected when using the split() method with a pipe character as the delimiter, given the string 'a|b|c|d|e|f'?

<p>['a', 'b', 'c', 'd', 'e', 'f'] (A)</p> Signup and view all the answers

Which method would you use to remove whitespace only from the start of a string?

<p>trimStart() (C)</p> Signup and view all the answers

If the below code is executed, what will be the value of myArray? let text = 'a b c d e f'; const myArray = text.split(' ');

<p>['a', 'b', 'c', 'd', 'e', 'f'] (B)</p> Signup and view all the answers

What does the 'onchange' event signify in HTML?

<p>An HTML element has been changed (A)</p> Signup and view all the answers

Which of the following is NOT a valid HTML event?

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

Which method is used to determine the length of a string in JavaScript?

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

What does the 'substr(start, length)' function do in JavaScript?

<p>Returns a substring based on a specified length (B)</p> Signup and view all the answers

What will the following code return: let text = 'Hello'; text[1];?

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

Which of the following correctly uses template literals for variable substitution?

<p>let message = <code>Welcome ${firstName}</code>; (D)</p> Signup and view all the answers

Which event should you use to execute a function after a page has finished loading?

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

What will the output of the following code be: let greeting = 'Hello World'; console.log(greeting.slice(0, 5));?

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

Which string declaration is correct in JavaScript?

<p>let site = 'w3schools.com'; (A), let site = &quot;w3schools.com&quot;; (D)</p> Signup and view all the answers

Flashcards

Constant Variables (const)

Variables declared with const cannot be redeclared or reassigned. They have block scope, meaning they are only accessible within the block where they are defined.

JavaScript Functions

A block of code designed to perform a specific task, executed when called (invoked).

JavaScript Objects

Flexible collections of key-value pairs, grouping related data (properties) and functions (methods).

Accessing Object Properties

Used to access properties of a JavaScript object, either using bracket notation or dot notation.

Signup and view all the flashcards

Object Methods

Functions belonging to an object, providing its behavior or actions.

Signup and view all the flashcards

HTML Events

Events are actions that happen either in the browser or triggered by user interactions, like clicking a button.

Signup and view all the flashcards

String

A sequence of characters that represents text. It can be defined using single or double quotes.

Signup and view all the flashcards

String Length Property

A property that tells you the length of a string. It returns the number of characters in the string.

Signup and view all the flashcards

onchange event

The 'onchange' event is triggered when the value of an HTML element, like an input field, is changed.

Signup and view all the flashcards

HTML Button

An HTML element that can be clicked by the user. It usually triggers an action when clicked.

Signup and view all the flashcards

onclick event

The 'onclick' event occurs when a user clicks on an HTML element.

Signup and view all the flashcards

onload event

The 'onload' event is triggered when the browser has completely finished loading the webpage.

Signup and view all the flashcards

onmouseover event

The 'onmouseover' event is triggered when the user hovers their mouse over an HTML element.

Signup and view all the flashcards

onmouseout event

The 'onmouseout' event occurs when the user moves their mouse away from an HTML element

Signup and view all the flashcards

onkeydown event

The 'onkeydown' event is triggered when a user presses a key on the keyboard.

Signup and view all the flashcards

String.slice()

Extracts a portion of a string and returns the extracted part as a new string.

Signup and view all the flashcards

String.slice() parameters

The first parameter indicates the starting position in the string (0-indexed). The second parameter indicates the ending position (not included), or if omitted, it slices to the end.

Signup and view all the flashcards

String.slice() negative indices

Negative values count from the end of the string. For example, -1 is the last character, -2 is the second to last, etc.

Signup and view all the flashcards

String.substr()

Extracts a portion of a string, similar to .slice(), but the second parameter specifies the length of the extracted part.

Signup and view all the flashcards

String.substr() omitting the second parameter

If the second parameter is omitted in .substr(), it extracts the rest of the string from the specified start position.

Signup and view all the flashcards

String.substr() negative index

The method counts the position from the end of the string when the first parameter is negative.

Signup and view all the flashcards

String.replace()

The method does not modify the original string. It creates and returns a new string with the replaced part.

Signup and view all the flashcards

String.replace() first match

The replace() method searches for and replaces only the first occurrence of the pattern within the string.

Signup and view all the flashcards

String.replace() case sensitivity

The replace() method is case-sensitive when matching patterns.

Signup and view all the flashcards

What is the purpose of replace() in JavaScript?

The replace() method in JavaScript allows you to substitute specific occurrences of a substring within a string with another substring. It modifies the original string and returns the updated string.

Signup and view all the flashcards

What is the concat() method used for in JavaScript?

The concat() method in JavaScript helps combine multiple strings into a single string. It effectively joins these strings together, creating a new string.

Signup and view all the flashcards

How do you convert a string to uppercase in JavaScript?

The toUpperCase() method converts all characters within a string to uppercase. It changes the case of the original string and returns the new uppercase string.

Signup and view all the flashcards

How do you convert a string to lowercase in JavaScript?

The toLowerCase() method converts all characters within a string to lowercase. It modifies the original string and returns the new lowercase string.

Signup and view all the flashcards

What does the trim() method do in JavaScript?

The trim() method in JavaScript removes whitespace characters from both ends of a string. It cleans up the string by removing leading and trailing spaces.

Signup and view all the flashcards

What does the split() method do in JavaScript?

The split() method in JavaScript is used to separate a string into an array of substrings based on a specified delimiter. It divides the string into chunks according to the provided separator.

Signup and view all the flashcards

Study Notes

JavaScript - Introduction - II

  • Constant Variables: Constants cannot be re-declared or reassigned. They have block scope.
  • Functions in JavaScript: A function is a block of code used for a particular task. It is executed when called or invoked. Functions can accept parameters.
  • JavaScript Objects: A JavaScript object is a versatile collection of key-value pairs (properties) and actions (methods). You use objects to group related data and behaviors. Objects are variables that have properties and functions.
  • Retrieving Object Properties: Accessing an object's property can be done in two ways: objectName["propertyName"] or objectName.propertyName

HTML Events

  • HTML Events: Events can be actions resulting from a user or happening within the browser, such as page loading, user clicks, mouse movements, form input changes, and button clicks. Specific JavaScript code is often associated with these events.

Strings in JavaScript

  • String Definition: Strings in JavaScript can use single or double quotes.
  • Quotes within Strings: Nested quotes are possible as long as the enclosing quotes differ in style.
  • Escape Characters: Special characters like apostrophes or backslashes can be included, using "".
  • String Properties and Methods: JavaScript strings have properties such as .length and methods for various operations like .slice(), .substring(), .substr(), .replace(), .concat(),.toUpperCase(), .toLowerCase(), .trim(), .trimStart(), .trimEnd(), .split()

String Functions

  • slice(start, end): A string function that extracts a portion of the string. If the end parameter is omitted, the rest of the string is extracted. The extracted string does not include the character at the end position
  • substring(start, end): Extracts a portion of a string from one index to another. It treats negative values as zero and does not include the final index
  • substr(start, length): Extracts characters starting at start for a specific length.

Variable Substitutions

  • Variable substitutions in strings use template literals (\${expression}``) to substitute variables directly
  • String concatenation can use the concat() method or a plus (+) operator

Additional notes

  • The example code for various JavaScript string functions has been omitted but is readily accessible via the example links given.
  • The presentation contained various examples of these JavaScript concepts.

Studying That Suits You

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

Quiz Team

Related Documents

Javascript II Lecture Notes PDF

Description

This quiz explores advanced concepts in JavaScript, including constant variables, functions, and objects. You'll learn how to retrieve object properties and understand HTML events alongside string handling in JavaScript. Dive deeper into the fundamentals of programming with this engaging assessment.

More Like This

JavaScript Objects and Arrays Quiz
10 questions
JavaScript Module 2: Objects
10 questions
JavaScript Built-in Objects Quiz
24 questions

JavaScript Built-in Objects Quiz

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Use Quizgecko on...
Browser
Browser