JavaScript Basics Quiz
48 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 the correct way to declare a variable named 'myVar' and assign it the value 'Hello World'?

  • var myVar = 'Hello World';
  • let myVar = 'Hello World'; (correct)
  • const myVar = 'Hello World';
  • variable myVar = 'Hello World';

Which data type is used to represent a value that is true or false?

  • null
  • boolean (correct)
  • string
  • number

What is the purpose of the const keyword in JavaScript?

  • To declare a variable whose value cannot be changed. (correct)
  • To define a function.
  • To create an object.
  • To declare a variable that can be reassigned.

What is the maximum value that can be represented by the number data type in Javascript?

<p>2^53 - 1 (D)</p> Signup and view all the answers

What is the purpose of using a switch statement in JavaScript?

<p>To execute different blocks of code based on the value of a variable. (D)</p> Signup and view all the answers

Which of the following is a valid variable name in JavaScript?

<p>myVariable$ (A), my_variable (D)</p> Signup and view all the answers

Which of the following code snippets correctly rewrites the given code using a single switch statement?

<pre><code class="language-javascript">let a = +prompt('a?', ''); switch (a) { case 0: alert(0); break; case 1: alert(1); break; case 2: alert('2,3'); break; case 3: alert('2,3'); break; } ``` (A) </code></pre> Signup and view all the answers

What is the output of the following code snippet?

<p>Hello, I'm JavaScript! (C)</p> Signup and view all the answers

What is the difference between null and undefined in Javascript ?

<p><code>null</code> represents the intentional absence of a value, while <code>undefined</code> represents a variable that has not been assigned a value. (A)</p> Signup and view all the answers

Which of the following is a NOT a valid comment in JavaScript?

<!-- This is a multi-line comment --> (D) Signup and view all the answers

Which of the following is a correct statement about functions in JavaScript?

<p>Functions are used to define the structure and behavior of a JavaScript program. (C)</p> Signup and view all the answers

What is the difference between a local variable and a global variable in JavaScript?

<p>Local variables are declared inside a function, while global variables are declared outside of any function. (A)</p> Signup and view all the answers

What is the difference between let and const in Javascript?

<p><code>let</code> is used to declare variables that can be reassigned, while <code>const</code> is used to declare variables that cannot be reassigned. (C)</p> Signup and view all the answers

What will the following code print? let i = 3; while (i) { alert( i-- ); }

<p>3, 2, 1 (B)</p> Signup and view all the answers

How can you convert the following code using a while loop? for (let i = 0; i < 3; i++) { alert( number ${i}! ); }

<p><code>let i = 0; while (i &lt; 3) {alert(</code>number ${i}!<code>); i++;}</code> (D)</p> Signup and view all the answers

What is the purpose of the break statement in a switch statement?

<p>It stops the execution of the entire <code>switch</code> statement. (D)</p> Signup and view all the answers

What output is produced by the following code? let a = 2 + 2; switch (a) { case 3: alert( 'Too small' ); break; case 4: alert( 'Exactly!' ); break; case 5: alert( 'Too big' ); break; default: alert( "I don't know such values " ); }

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

Which of the following code snippets accurately converts the provided switch statement to an equivalent if...else statement?

switch (browser) {
case 'Edge':
alert( "You've got the Edge!" );
break;
case 'Chrome':
case 'Firefox':
case 'Safari':
case 'Opera':
alert( 'Okay we support these browsers too ' );
break;
default:
alert( 'We hope that this page looks ok!' );
}

<pre><code class="language-javascript"> if (browser === 'Edge') { alert( &quot;You've got the Edge!&quot; ); } else if (browser === 'Chrome' || browser === 'Firefox' || browser === 'Safari' || browser === 'Opera') { alert( 'Okay we support these browsers too ' ); } else { alert( 'We hope that this page looks ok!' ); }``` (C) </code></pre> Signup and view all the answers

When writing code to find prime numbers, what is the best way to check if a number is divisible by another number?

<p>Using the modulo operator (%) to check if the remainder is zero. (C)</p> Signup and view all the answers

What is the purpose of the default case in a switch statement?

<p>It is executed only if none of the preceding <code>case</code> values match the <code>switch</code> expression. (A)</p> Signup and view all the answers

What is the most efficient method to check if a number n is prime in a computer program?

<p>Using the <code>Math.sqrt</code> function to check for divisibility by numbers up to the square root of <code>n</code>. (A)</p> Signup and view all the answers

What is the correct implementation of a while loop to output prime numbers from 2 to n? Assume all necessary variables are declared.

<pre><code class="language-javascript"> while (i &lt;= n) { if (isPrime(i)) { alert(i); } i++; } ``` (C) </code></pre> Signup and view all the answers

What will the output be when executing the code alert(1 && null && 2)?

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

What does the nullish coalescing operator '??' return when both operands are defined?

<p>The first operand (D)</p> Signup and view all the answers

Which part of a for loop can be omitted?

<p>All parts can be omitted (B)</p> Signup and view all the answers

In a while loop, when does the loop typically exit?

<p>When the condition becomes falsy (A)</p> Signup and view all the answers

What is the output of the code alert(alert(1) && alert(2));?

<p>1, 2, and then undefined (C)</p> Signup and view all the answers

What will the following loop output? for (let i = 0; i < 3; i++) { alert(i); }

<p>0, 1, 2 (D)</p> Signup and view all the answers

How does the continue statement affect loop iterations?

<p>It skips to the next iteration (B)</p> Signup and view all the answers

When using a do...while loop, under what condition will the loop execute at least once?

<p>When the condition is initially false (B)</p> Signup and view all the answers

What does JavaScript primarily enable developers to do on web pages?

<p>Implement complex features (A)</p> Signup and view all the answers

Which of the following is true about the ECMA-262 specification?

<p>It provides detailed guidelines on JavaScript language (C)</p> Signup and view all the answers

In which environments can JavaScript execute?

<p>In the browser and on the server (B)</p> Signup and view all the answers

Which JavaScript engine is used in Firefox?

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

What feature does TypeScript add to JavaScript?

<p>Strict data typing (D)</p> Signup and view all the answers

What file extension is used for a JavaScript file?

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

How can you run a JavaScript code snippet directly in a web browser?

<p>By opening the Developer Console (A)</p> Signup and view all the answers

What does the command console.log('Hello World'); do?

<p>Prints 'Hello World' to the console (A)</p> Signup and view all the answers

What is the primary role of statements in JavaScript?

<p>To perform actions and commands (B)</p> Signup and view all the answers

Which of the following is a feature of CoffeeScript?

<p>It introduces a shorter syntax (B)</p> Signup and view all the answers

What will the variable 'age' contain after executing 'let age = prompt("How old are you?", 100);'?

<p>The user input value as a string (B)</p> Signup and view all the answers

What does the confirm function return when the Cancel button is pressed?

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

What type of conversion occurs automatically when using alert with a boolean value?

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

What will the output of 'alert("6" / "2");' be?

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

What happens to a value of 'null' during numeric conversion?

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

When is explicit conversion necessary?

<p>When converting values for specific computation needs (A)</p> Signup and view all the answers

What is the result of executing 'let value = true; value = String(value);'?

<p>The string &quot;true&quot; (B)</p> Signup and view all the answers

Which of the following statements about string conversion is correct?

<p>Strings can be explicitly converted using String(value) (D)</p> Signup and view all the answers

Flashcards

Number

A data type that represents numbers, including both integers and floating-point numbers.

String

A data type that represents characters, such as letters, numbers, and symbols.

Boolean

A data type representing truth values, either true or false.

Null

A data type that represents the absence of a value or an unknown value.

Signup and view all the flashcards

Undefined

A data type that represents a variable that has not been assigned a value.

Signup and view all the flashcards

Object

A data type for storing collections of key-value pairs.

Signup and view all the flashcards

Symbol

A data type for creating unique identifiers for objects. They are immutable and are not coerced to strings.

Signup and view all the flashcards

BigInt

A data type that represents arbitrarily large integers, useful for handling numbers beyond the normal limits of the number data type.

Signup and view all the flashcards

What is JavaScript?

A scripting or programming language used to create interactive web pages.

Signup and view all the flashcards

What is the ECMA-262 specification?

A set of rules and guidelines that define how the JavaScript language works.

Signup and view all the flashcards

What is a JavaScript Engine?

A program that executes JavaScript code.

Signup and view all the flashcards

Where can JavaScript execute?

The ability of a JavaScript engine to run on a computer.

Signup and view all the flashcards

What is TypeScript?

A programming language that is transpiled into JavaScript. It simplifies the development process by providing a more concise syntax and options for data typing.

Signup and view all the flashcards

What is a statement in Javascript?

A code snippet that performs an action.

Signup and view all the flashcards

What is a browser's developer console?

A tool that helps developers create and debug websites.

Signup and view all the flashcards

What is the 'alert()' function?

A method used to display a message in a pop-up window.

Signup and view all the flashcards

What is the 'console.log()' function?

A method for displaying information in the browser's console.

Signup and view all the flashcards

What is a <script> tag?

A method for embedding JavaScript code within an HTML file.

Signup and view all the flashcards

alert()

Displays a message to the user and waits for them to click 'OK'.

Signup and view all the flashcards

prompt()

Shows a dialog box that allows the user to input a value. Returns the entered value as a string.

Signup and view all the flashcards

confirm()

Shows a dialog box with a question and two buttons: 'OK' and 'Cancel'. Returns true if 'OK' is pressed and false otherwise.

Signup and view all the flashcards

String Conversion

The automatic conversion of a value to a string. Often happens when displaying values using alert() or in string concatenation.

Signup and view all the flashcards

Numeric Conversion

The automatic conversion of a value to a number. Often happens in mathematical operations.

Signup and view all the flashcards

String(value)

A function that explicitly converts a value to a string.

Signup and view all the flashcards

Number(value)

A function that explicitly converts a value to a number.

Signup and view all the flashcards

Numeric Conversion Rules

The rules that determine how various values are converted to numbers.

Signup and view all the flashcards

Function

A built-in JavaScript construct that performs a specific action or task.

Signup and view all the flashcards

Function body

A section of code within a function that is executed when the function is called.

Signup and view all the flashcards

Variable scope

The place where a variable is declared and can be accessed.

Signup and view all the flashcards

Local variable

A variable declared inside a function, making it accessible only within that function's scope.

Signup and view all the flashcards

Parameters

The values passed to a function when it's called.

Signup and view all the flashcards

While loop

A loop that repeatedly executes a block of code as long as a given condition is true. It checks the condition before each iteration and only executes the code if the condition is true.

Signup and view all the flashcards

For loop

A loop that repeatedly executes a block of code a specific number of times. It initializes a counter variable, checks a condition, and updates the counter variable in each iteration.

Signup and view all the flashcards

What is a Prime Number?

A number greater than 1 that is only divisible by 1 and itself. For example, 2, 3, 5, 7 are prime numbers.

Signup and view all the flashcards

Switch statement

A conditional statement that allows you to execute different code blocks based on the value of an expression. It evaluates an expression and matches it with different cases, then executes the code corresponding to the matching case.

Signup and view all the flashcards

Break statement

A statement used to immediately exit a loop or switch statement and continue execution from the next statement after the loop or switch.

Signup and view all the flashcards

Do-while loop

A loop that continues to iterate unless the condition is met. It first executes the loop then checks the condition.

Signup and view all the flashcards

What is the nullish coalescing operator (??) and how does it work?

The nullish coalescing operator (??) is a logical operator that returns its first operand if it is not null or undefined. Otherwise, it returns its second operand. It is used for providing default values when the value of a variable might be undefined.

Signup and view all the flashcards

What is a 'for' loop and what are its parts?

The 'for' loop is a control flow statement that allows code to be executed repeatedly. It has three parts: initialization, condition, and increment/decrement. The loop continues to iterate as long as the condition is true.

Signup and view all the flashcards

What is a 'while' loop and how does it work?

The 'while' loop is a control flow statement that repeatedly executes a block of code as long as a condition is true. It is used to execute the code within the loop until a specific condition is met.

Signup and view all the flashcards

What is a 'do...while' loop and what makes it different from a 'while' loop?

The 'do...while' loop is a variation of the 'while' loop that guarantees the code within the loop is executed at least once before the condition is checked. This loop executes the block of code once and then keeps executing as long as the condition is true.

Signup and view all the flashcards

What does the 'break' statement do?

The 'break' statement immediately exits the current loop, regardless of the loop's condition. This is used to terminate the execution of the loop prematurely.

Signup and view all the flashcards

What does the 'continue' statement do?

The 'continue' statement skips the remaining code within the current iteration of the loop and jumps to the next iteration. This is used to skip certain iterations based on a specific condition.

Signup and view all the flashcards

What is the logical AND operator (&&) and how does it work?

The logical AND operator (&&) returns the first operand if it is falsy. Otherwise, it returns the second operand.

Signup and view all the flashcards

What is the logical OR operator (||) and how does it work?

The logical OR operator (||) returns the first operand if it is truthy. Otherwise, it returns the second operand.

Signup and view all the flashcards

Study Notes

Introduction to JavaScript

  • JavaScript is a scripting or programming language used to implement complex features on web pages.
  • The core language provides a minimal API for working with numbers, text, arrays, sets, etc., but input/output and more sophisticated features (networking, storage, graphics) are handled by the "host environment" (like a browser).
  • The ECMA-262 specification details the JavaScript language.

Where JavaScript Runs

  • JavaScript can run in a web browser, on a server, or on any device with a JavaScript engine.

JavaScript Engine

  • Browsers embed a JavaScript engine (sometimes called a "JavaScript virtual machine").
  • Examples of JavaScript engines include V8 (in Chrome, Opera, and Edge), SpiderMonkey (in Firefox), Chakra (in IE), and JavaScriptCore, Nitro, and SquirrelFish (in Safari).

Languages Transpiled to JavaScript

  • CoffeeScript uses a shorter syntax for clearer and more precise code.
  • TypeScript adds strict data typing for complex systems development and support.
  • Flow also adds data typing, and is developed by Facebook.
  • Dart can be transpiled to JavaScript by Google.
  • Brython is a Python transpiler to JavaScript that allows applications in pure Python.
  • Kotlin can target the browser or Node.js.

Tools

  • IDE: VS Code
  • Browsers: Chrome, Firefox, Edge
  • Developer Console: Ctrl + Shift + J (in browsers) to access tools like 'Elements' and 'Console'.

Getting Started

  • Create an index.html file.
  • Add the provided JavaScript code to the file.

Hello World Example

  • Basic JavaScript code for displaying "Hello, world!" using an alert box.
  • Demonstrates creating an HTML file with JavaScript code.

Ways to Use JavaScript

  • Include the <script> tag within the <head> or <body> section of an HTML file.
  • Use a separate JavaScript file and link it to HTML using <script src="filename.js"> </script>.

Statements

  • Statements are constructs and commands for actions in JavaScript.
  • Semicolons at the end of statements are not mandatory but are recommended.

Comments

  • One-line comments begin with //.
  • Multiline comments span from /* to */.

Variables

  • Use the let keyword to create variables.
  • Variables must use letters, numbers, or symbols $ and _ in the name and the name cannot start with a number. Names can't contain hyphens.
  • Example: let message = 'Hello!';

Constants

  • Use the const keyword to declare constants.
  • Constant values cannot be reassigned.

Data Types

  • Number: Represents integers and floating-point numbers, with limitations.
  • BigInt: Handles integers of arbitrary length.
  • String: Stores characters, with support for double quotes, single quotes, and backticks(templating).
  • Boolean: true or false.
  • Null: Represents a lack of value or absence of data.
  • Undefined: Indicates a variable has been declared but not assigned a value.
  • Object: Used for complex data structures.
  • Symbol: Represents unique identifiers (useful for objects).

The typeof Operator

  • Returns a string indicating the type of an operand.

Interaction: alert, prompt, confirm

  • alert: Displays a message.
  • prompt: Displays a dialog box for user input.
  • confirm: Displays a dialog box for a yes/no question.

Type Conversions

  • Implicit type conversions are often used in JavaScript (e.g., alert automatically converts values to strings).
  • Explicit conversions of values to different types are possible using the Number, String, and Boolean functions.
    • Rules exist for specific values (e.g., undefined becomes NaN when converted to a number).

Math Operations

  • Basic mathematical operations such as addition, subtraction, multiplication, division, remainder, and exponentiation (+, -, *, /, %, **).

String Concatenation

  • Using the binary + operator for merging strings.

Increment/Decrement Operators

  • The ++ operator increments a variable by 1.
  • The -- operator decrements a variable by 1.

Comparisons

  • Greater than (>), less than (<), equals (==), greater than or equals (>=), less than or equals (<=).
  • Strict equality (===) checks for both value and type equality.

The if Statement

  • Executes a block of code based on a condition.

The if...else Statement

  • Executes one block if the condition is true, another block if false.

The if...else if...else Statement

  • Allows checking multiple conditions.

Conditional Operator (? :)

  • A shorthand way to express conditional statements.

Loops: while, do...while, for

  • while: Repeats code as long as a condition is true.
  • do...while: Repeats code at least once; then repeats as long as a condition is true.
  • for: Efficient repetition for a set number of iterations.

Loop Control Statements: break, continue

  • break: Exits a loop prematurely.
  • continue: Skips the current iteration of a loop.

The switch Statement

  • A more structured method for handling multiple conditions (alternatives to multiple if-else if).

Functions

  • JavaScript functions are self-contained blocks of code to execute specific tasks.
  • Functions can take parameters and return values.

Function Expressions

  • Function expressions provide a way to create functions that are stored as variables or used in other expressions.

Arrow Functions

  • A more concise way to create functions (often used as callback functions).

Callback Functions

  • Pass functions as arguments to other functions.

References

  • Provides information on where to find additional resources concerning JavaScript.

Studying That Suits You

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

Quiz Team

Related Documents

Introduction to Javascript PDF

Description

Test your knowledge of essential JavaScript concepts, including variable declaration, data types, and function definitions. This quiz covers key topics such as the use of keywords, switch statements, and differences between null and undefined in JavaScript.

More Like This

JavaScript Variables
10 questions

JavaScript Variables

SophisticatedStatueOfLiberty avatar
SophisticatedStatueOfLiberty
JavaScript Basics Quiz
24 questions

JavaScript Basics Quiz

AdvantageousPurple8633 avatar
AdvantageousPurple8633
Use Quizgecko on...
Browser
Browser