Podcast
Questions and Answers
What is the data type of the variable txt4 after the execution of the given statements?
What is the data type of the variable txt4 after the execution of the given statements?
What is the result of the expression '5' + 5 in JavaScript?
What is the result of the expression '5' + 5 in JavaScript?
What is the purpose of the conditional operator in JavaScript?
What is the purpose of the conditional operator in JavaScript?
What is the syntax to define a JavaScript function?
What is the syntax to define a JavaScript function?
Signup and view all the answers
What is the data type of the variable x after the statement 'x = 5 + 5'?
What is the data type of the variable x after the statement 'x = 5 + 5'?
Signup and view all the answers
What is the purpose of logical operators in JavaScript?
What is the purpose of logical operators in JavaScript?
Signup and view all the answers
What is the purpose of the assignment operator in JavaScript?
What is the purpose of the assignment operator in JavaScript?
Signup and view all the answers
What is the data type of the variable x after the statement 'x = 5 + "5"'?
What is the data type of the variable x after the statement 'x = 5 + "5"'?
Signup and view all the answers
What is the purpose of the return statement in a function?
What is the purpose of the return statement in a function?
Signup and view all the answers
What is the correct syntax for declaring a function with three parameters?
What is the correct syntax for declaring a function with three parameters?
Signup and view all the answers
What is the output of the following code: document.write(product(4,3))?
What is the output of the following code: document.write(product(4,3))?
Signup and view all the answers
What is the purpose of the if statement in JavaScript?
What is the purpose of the if statement in JavaScript?
Signup and view all the answers
What is the correct syntax for an if statement in JavaScript?
What is the correct syntax for an if statement in JavaScript?
Signup and view all the answers
What will happen if you use uppercase letters (IF) instead of lowercase letters (if) in an if statement?
What will happen if you use uppercase letters (IF) instead of lowercase letters (if) in an if statement?
Signup and view all the answers
What is the purpose of the switch statement in JavaScript?
What is the purpose of the switch statement in JavaScript?
Signup and view all the answers
What is the correct way to declare a variable in JavaScript?
What is the correct way to declare a variable in JavaScript?
Signup and view all the answers
What is a characteristic of JavaScript variable names?
What is a characteristic of JavaScript variable names?
Signup and view all the answers
What is the scope of a variable declared within a JavaScript function?
What is the scope of a variable declared within a JavaScript function?
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'
?
What is the result of the expression txt3=txt1+txt2
assuming txt1='What a very'
and txt2='nice day'
?
Signup and view all the answers
What is the purpose of assignment operators in JavaScript?
What is the purpose of assignment operators in JavaScript?
Signup and view all the answers
What is the difference between local and global variables in JavaScript?
What is the difference between local and global variables in JavaScript?
Signup and view all the answers
What is the result of the expression alert(' Global y = ' + y)
assuming y=5
?
What is the result of the expression alert(' Global y = ' + y)
assuming y=5
?
Signup and view all the answers
What is the purpose of the var
keyword in JavaScript?
What is the purpose of the var
keyword in JavaScript?
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'
?
What is the result of the expression txt4=txt1+' '+txt2
assuming txt1='What a very'
and txt2='nice day'
?
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 < 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.
Description
This quiz covers the basics of functions in programming, including the syntax and usage of function definitions and the return statement.