JS Midterms

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

In JavaScript, values are considered "truthy" if they are treated as true in a Boolean context. In other words, when a value is used in a condition, it is considered truthy if it evaluates to true, and "falsy" if it evaluates to false. Here's a list of values that are considered truthy in JavaScript:

- Non-empty strings: Any string with one or more characters is considered truthy. - Numbers (except 0): Any numeric value (positive or negative) other than 0 is considered truthy. true: The Boolean value true is always truthy. - Objects: Any JavaScript object, including arrays and functions, is considered truthy. - Arrays: Even empty arrays are considered truthy because they are objects. - Functions: Functions are considered truthy. - Non-empty arrays: Arrays with one or more elements are considered truthy. - Custom objects: Objects you create, if they are not specifically designed to be falsy, are usually truthy.

Global Scope:

- Variables and functions declared in the global scope are accessible from any part of your JavaScript code. - Global variables and functions are defined outside of any functions or code blocks. - They can be accessed by any function or code block, as well as in the global context. - Global scope is accessible throughout your entire program. - Be cautious with global variables to avoid naming conflicts and unintentional changes to global state.

Local (Function) Scope:

- Variables and functions declared inside a function are considered to have local scope. - They are only accessible within the function where they are defined. - Local scope provides a way to encapsulate variables and functions, preventing naming conflicts with global scope. - Each function creates its own isolated scope.

You can declare a variable using the let, var and const keywords. Explain how variables differ when using each keyword

- const is used to declare variables that are constant. - let is used to declare variables that follow normal scope rules within a code segment (i.e. if you define a variable within an if-statement or loop, you cannot access the variable outside that structure). - var does not follow normal scope rules as described above

In JavaScript, the following values are considered "falsy," which means they are treated as equivalent to false in Boolean contexts:

- false: The boolean value false itself is falsy. - 0: The integer 0 is falsy. - -0: Negative zero is also falsy. - 0n: BigInt zero is falsy. - "" (empty string): An empty string is falsy. - null: The null value is falsy. - undefined: The undefined value is falsy. - NaN: Not-a-Number is falsy. It represents the result of an invalid mathematical operation. - document.all: In some older browsers, document.all is a non-standard property that is falsy.

Each JavaScript file ends with a(n) ____________ extension.

.js

True or false. The following code segment will run without any errors. console.log("There is a "lesson" to be learned there...");

False, you cannot nest double quotes in a double-quoted string.

Web pages and their associated CSS & JavaScript files are transferred to a browser using the______________ network protocol

HTTP

True or false When calling a function you can ignore the parameters of the function and pass any number of arguments.

True

True or false You can define a function in JavaScript without a name.

True

True or false. An HTML page can reference more than one JavaScript file.

True

True or false. The order in which you reference JavaScript files can determine the outcome of JavaScript statements in a program.

True

Will the following code execute without errors? let sport = "golf"; let fan = false; //who needs curly braces? if (sport == "hockey" && fan == true) alert("You're a hockey fan!"); else if (sport == "golf" && fan == false) alert("You're not a fan of golf."); else alert("Are you a fan of " + sport + "? " + fan);

Yes, you can omit curly braces if there is only a single line within a block inside an if statement.

When writing JavaScript programs we must follow the naming restrictions of the language. Name three rules that you can not break when choosing variable names.

You can only use alpha-numeric characters. You can't include white space characters (spaces, new lines, tabs, etc...) You can't start with a number.

=== OR !==

`===` (Strict Equality Operator): The === operator checks for equality without performing type coercion. It requires both the value and the type of the operands to be the same for the comparison to be true. For example, 5 === "5" is false because the types are different (number vs. string).

== OR !=

`==` (Abstract Equality Operator): The == operator checks for equality after performing type coercion if the types of the operands are different. It tries to make the values on both sides of the operator the same type before making the comparison. For example, 5 == "5" is true because the string is coerced to a number before comparison.

Perform the following operations in JavaScript by following the precendence and operator rules ofthe language. Assume: a = 5, b = 3, c = false, d = true a == b && a != b b * 5 / a + 5 b * 5 / (a + 5) !d || b - 2 > 0

a == b && a != b -> false b * 5 / a + 5 -> 8 b * 5 / (a + 5) -> 1.5 !d || b - 2 > 0 -> true

What is the difference between an argument and parameter in programming nomenclature?

a parameter is the variable that holds inputs to a function, whereas an argument is the value that is passed in

Which of the following notations are supported for JavaScript comments? a. //my comment b. /* my comment */ c. #my comment d. <!-- my comment -->

a. //my comment b. /* my comment */

Given that the variable num stores the number 2, what is the output of the following expressions. a. 7 % num b. num ** 4 c. num / 2 / 2

a. 7 % num = 1 (this is the remainder after 7 / 2) b. num ** 4 = 16 (this is an exponent 24) c. num / 2 / 2 = 0.5

What are the naming conventions for the following elements in a JavaScript program? a. Functions b. Variables

a. Functions - camel case b. Variables - camel case

JavaScript is said to be a ____________ typed language. a. Loosely b. Strongly

a. Loosely

Name the three functions we call for the following types of dialog boxes. a. Sending a message to a user. b. Asking the user a yes/no question. c. Asking the user to type a string value.

a. Sending a message to a user. -> alert() b. Asking the user a yes/no question. -> confirm() c. Asking the user to type a string value. -> prompt()

Which of the following types of values are considered falsey in JavaScript? a. false b. 100 c. undefined d. [1, 2, 3] //non-empty array e. [] //empty array f. 0 //zero g. "hello" //non-empty string h. "" //the empty string

a. false c. undefined e. [] //empty array f. 0 //zero h. "" //the empty string

Which of the following data types does JavaScript support to describe missing or invalid data? a. nil b. null c. invalid d. NaN e. undefined

b. null d. NaN e. undefined

While semi-colons can often be ignored in JavaScript's language, which of the following loops makes use of a semi-colon? a. For-loop b. While-loop c. Do-while-loop

c. Do-while-loop

Which of the following are data types in JavaScript? a. Integer b. Float c. Number d. Character e. String f. Array g. Object h. Boolean

c. Number

Each browser supports a developer tool, called a ____________, that lets you walk through your code line-by-line as it is executing. a. interpreter b. inspector c. debugger d. IDE

c. debugger

Which of the following are considered client-side languages in web-development? a. HTML b. CSS c. PHP d. JavaScript e. Java

d. JavaScript

The _________________ is the organizational model that your browser uses to track elements on an HTML page

document object model (DOM)

True or false Two functions can be defined with the same name in a script, provided they have different parameter lists.

false, there is no function overloading in JavaScript

Each function defined in a script must begin with the _________________ keyword.

function

Name the two types of scope a variable can have in JavaScript

global & local scope

Write a single line of code that selects an HTML element on a page using the elements ID attribute. Store the HTML element in a JavaScript variable for later use. a. Note: Make sure to store the HTML element in a JavaScript variable for later use.

let element = document.getElementById("myid"); let thisAlsoWorks = document.querySelector("#myid");

Write a single line of code that selects an HTML element on a page using the element type (i.e. p, img, span, etc...). You can assume that there is only a single element of that type on the page. a. Note: Make sure to store the HTML element in a JavaScript variable for later use.

let list = document.querySelector("ul");

he rules of a programming language are called the _________________ of the language.

syntax

True or false You can define a function in JavaScript that accepts any number of parameters.

true, you would use the arguments array to hold the values.

What value is stored in a variable if you try to access it after you have declared the variable but before you have assigned it?

undefined

Show two alternative ways in which you can write: x = x + 1;

x += 1 x++; ++x;


Ensembles d'études connexes

Ancient Greece: The Persian Empire

View Set

Basic Medical Terms To Describe Disease Conditions

View Set

Real Estate Principals Practice Exam

View Set

Chapter 4 The Art of Communication

View Set