Functions in Programming
24 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 data type of the variable txt4 after the execution of the given statements?

  • Boolean
  • Number
  • String (correct)
  • Null
  • What is the result of the expression '5' + 5 in JavaScript?

  • 10
  • 55 (correct)
  • Undefined
  • Error
  • What is the purpose of the conditional operator in JavaScript?

  • To perform arithmetic operations
  • To assign a value to a variable based on some condition (correct)
  • To declare a variable
  • To output a message to the user
  • What is the syntax to define a JavaScript function?

    <p>function functionname()</p> Signup and view all the answers

    What is the data type of the variable x after the statement 'x = 5 + 5'?

    <p>Number</p> Signup and view all the answers

    What is the purpose of logical operators in JavaScript?

    <p>To determine equality or difference between variables or values</p> Signup and view all the answers

    What is the purpose of the assignment operator in JavaScript?

    <p>To assign a value to a variable</p> Signup and view all the answers

    What is the data type of the variable x after the statement 'x = 5 + "5"'?

    <p>String</p> Signup and view all the answers

    What is the purpose of the return statement in a function?

    <p>To specify the value that a function should return</p> Signup and view all the answers

    What is the correct syntax for declaring a function with three parameters?

    <p>function functionName(parameter1, parameter2, parameter3)</p> Signup and view all the answers

    What is the output of the following code: document.write(product(4,3))?

    <p>16</p> Signup and view all the answers

    What is the purpose of the if statement in JavaScript?

    <p>To execute a block of code only if a condition is true</p> Signup and view all the answers

    What is the correct syntax for an if statement in JavaScript?

    <p>if(condition) {code to be executed}</p> Signup and view all the answers

    What will happen if you use uppercase letters (IF) instead of lowercase letters (if) in an if statement?

    <p>The code will generate a JavaScript error</p> Signup and view all the answers

    What is the purpose of the switch statement in JavaScript?

    <p>To select one of many blocks of code to be executed</p> Signup and view all the answers

    What is the correct way to declare a variable in JavaScript?

    <p>var d = new Date(); var time = d.getHours();</p> Signup and view all the answers

    What is a characteristic of JavaScript variable names?

    <p>They must begin with a letter, an underscore, or a dollar sign</p> Signup and view all the answers

    What is the scope of a variable declared within a JavaScript function?

    <p>Local</p> Signup and view all the answers

    What is the result of the expression txt3=txt1+txt2 assuming txt1='What a very' and txt2='nice day'?

    <p>What a verynice day</p> Signup and view all the answers

    What is the purpose of assignment operators in JavaScript?

    <p>To assign values to variables</p> Signup and view all the answers

    What is the difference between local and global variables in JavaScript?

    <p>Local variables can only be accessed within a function, while global variables can be accessed anywhere</p> Signup and view all the answers

    What is the result of the expression alert(' Global y = ' + y) assuming y=5?

    <p>Global y = 5</p> Signup and view all the answers

    What is the purpose of the var keyword in JavaScript?

    <p>To declare a local variable</p> Signup and view all the answers

    What is the result of the expression txt4=txt1+' '+txt2 assuming txt1='What a very' and txt2='nice day'?

    <p>What a very nice day</p> Signup and view all the answers

    Study Notes

    Functions in JavaScript

    • A function is a block of code designed to perform a particular task and can be executed by an event or by a call to the function.
    • A JavaScript function is defined with the function keyword, followed by a name, and then parentheses ().
    • Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

    Function Syntax

    • function functionName(parameter1, parameter2, …) { code to be executed }

    Return Statement

    • Functions that return a value must use the return statement.
    • Example: function product(a, b) { return a * b; }

    Conditional Statements

    • if statement: executes code if a specified condition is true.
    • if...else statement: executes code if the condition is true and another code if the condition is false.
    • if...else if....else statement: selects one of many blocks of code to be executed.
    • switch statement: selects one of many blocks of code to be executed.

    Conditional Statement Syntax

    • if (condition) { code to be executed if condition is true }

    Example of if Statement

    • if (time &lt; 12) { write a "Good morning" greeting }

    Adding Strings and Numbers

    • When you add a number and a string, the result will be a string.

    Examples of Adding Strings and Numbers

    • var x = 5 + 5;
    • document.write(x + "");
    • x = "5" + "5";
    • document.write(x + "");
    • x = 5 + "5";
    • document.write(x + "");
    • x = "5" + 5;
    • document.write(x + "");

    Comparison Operators

    • Used in logical statements to determine equality or difference between variables or values.

    Logical Operators

    • Used to combine conditions in logical statements.

    Conditional Operator

    • Assigns a value to a variable based on some condition.
    • Syntax: variablename = (condition) ? value1 : value2;

    Variables in JavaScript

    • Variable names are case sensitive.
    • Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
    • Subsequent characters can be letters, digits, underscores, or dollar signs.

    Local and Global Variables

    • Local variables: declared within a JavaScript function and can only be accessed within that function.
    • Global variables: declared outside a function and can be accessed by all scripts and functions on the web page.

    Example of Local and Global Variables

    • function displaymessage() { var x = 3; alert("Local x = " + x + " , Global y=" + y); }
    • var y = 5; alert(" Global y = " + y);

    Arithmetic Operators

    • Used for arithmetic operations such as addition, subtraction, multiplication, division, etc.

    Assignment Operators

    • Used to assign values to JavaScript variables.

    The + Operator

    • Can be used to add string variables or text values together.
    • Example: txt1 = "What a very"; txt2 = "nice day"; txt3 = txt1 + txt2; txt4 = txt1 + " " + txt2;

    Studying That Suits You

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

    Quiz Team

    Description

    This quiz covers the basics of functions in programming, including the syntax and usage of function definitions and the return statement.

    More Like This

    Algebra 1 STAAR EOC Flashcards
    21 questions
    Identifying Functions Flashcards
    16 questions
    Functions and Their Graphs
    8 questions

    Functions and Their Graphs

    AdulatoryBinomial245 avatar
    AdulatoryBinomial245
    Functions and Relations Quiz
    10 questions

    Functions and Relations Quiz

    EquitableSmokyQuartz1424 avatar
    EquitableSmokyQuartz1424
    Use Quizgecko on...
    Browser
    Browser