JavaScript Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is a characteristic of JavaScript?

  • Database management
  • Server-side scripting only
  • Operating system development
  • Client-side scripting (correct)

In what year was JavaScript created?

  • 2005
  • 2000
  • 1990
  • 1995 (correct)

What name was JavaScript originally released under?

  • ECMAScript
  • JScript
  • Java
  • LiveScript (correct)

What standard was JavaScript standardized as in 1997?

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

Which of the following can be used to include JavaScript in an HTML document?

<p><code>&lt;script&gt;</code> tag (A)</p> Signup and view all the answers

Which of the following is generally considered the best location for placing <script> tags in HTML?

<p>End of the <code>&lt;body&gt;</code> section (C)</p> Signup and view all the answers

Which property is used to insert dynamic content into an HTML element using Javascript?

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

What are JavaScript statements composed of?

<p>Keywords, operators, values, and expressions (B)</p> Signup and view all the answers

What term is used for values stored inside a JavaScript variable?

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

Which keyword is used to declare a variable whose value cannot be changed after it is assigned?

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

JavaScript code can only be included directly in an HTML document.

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

The innerText property is used to insert text into an HTML element in Javascript.

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

In Javascript, ECMAScript is the official standard name for Javascript.

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

Microsoft created JavaScript.

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

A JavaScript statement is a series of instructions generally executed in top-to-bottom order.

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

JavaScript ignores tabs and spaces.

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

The const keyword is used to declare variables that can be reassigned.

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

Variable names cannot contain numbers.

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

Functions in Javascript must have a name.

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

The parenthesis operator () calls the function.

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

Given that JavaScript is a loosely typed language, what is the primary implication of this characteristic?

<p>Variables can change data type during the script execution. (A)</p> Signup and view all the answers

Which of the following code snippets demonstrates the correct usage of parseInt() to convert a string to an integer?

<p><code>parseInt(&quot;42&quot;)</code> (B)</p> Signup and view all the answers

What is the significance of function hoisting in JavaScript?

<p>It allows function declarations to be called before they are defined. (C)</p> Signup and view all the answers

In JavaScript, what is the primary difference between == and ===?

<p><code>==</code> compares values but allows type coercion, while <code>===</code> compares values and data types without type coercion. (D)</p> Signup and view all the answers

What will be the output of console.log(typeof(42 + ''))?

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

Which of the following best describes the concept of lexical scope in JavaScript?

<p>The scope of a variable is determined by its location within the source code. (A)</p> Signup and view all the answers

Given the following code:

let x = 10;
function modifyX() {
  x = 20;
}
modifyX();
console.log(x);

What will be logged to the console?

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

Which of the following is NOT a valid use case for comments in JavaScript?

<p>Defining variable names. (D)</p> Signup and view all the answers

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

<p>To end the execution of the function and specify a value to be returned to the caller. (C)</p> Signup and view all the answers

Given the code snippet let result = '10' + 5;, what is the value and type of result?

<p>Value: <code>105</code>, Type: <code>string</code> (A)</p> Signup and view all the answers

In JavaScript, function names can only contain letters and digits, excluding underscores and dollar signs.

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

ECMAScript is not still commonly referred to as JavaScript.

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

In JavaScript, the typeof keyword can be used to determine the initial data type of a variable, but not after it has been changed later in the script.

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

JavaScript is exclusively a server-side scripting language to enhance web page responsiveness.

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

In JavaScript, the const keyword is used to declare variables that can be reassigned new values throughout the script.

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

Including JavaScript in the <head> section is the best practice to ensure optimal page load times for scripts.

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

In Javascript, '42' + 8 will result in the numerical value 50.

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

Whitespace such as spaces, tabs, and line breaks are significant in JavaScript and can affect code execution.

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

Semicolons are mandatory in all modern Javascript code, even if there is only a single statement on each line.

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

If the conversion of a string to a number using parseInt() or parseFloat() fails, JavaScript returns Null.

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

Flashcards

What is JavaScript?

A scripting language embedded in web browsers that enables client-side scripting, improving web page responsiveness.

How to include JavaScript?

To use JavaScript code in an HTML document, embed it between <script> tags.

What is function?

A named block of JavaScript code designed to perform a particular task, and it returns a final single value when invoked.

What are keywords?

Keywords are reserved words with special significance in JavaScript.

Signup and view all the flashcards

What are operators?

Special characters that perform an operation on one or more operands.

Signup and view all the flashcards

What is a Variable?

A container in which data can be stored and retrieved later.

Signup and view all the flashcards

Keywords

Words that have special significance in the Javascript language.

Signup and view all the flashcards

Global Scope means...

Variable that is accessible from anywhere in the code. Declared OUTSIDE of function.

Signup and view all the flashcards

Function Hoisting

Function can be called before its declaration in the code because the javascript engine 'hoists' the declaration to the top during execution.

Signup and view all the flashcards

Equality and Inequality Operators

Always prefer '===' and '!==' because they ensure strict equality by checking both value and data type.

Signup and view all the flashcards

Writing to an HTML Element

Writing content dynamically into an HTML element using JavaScript.

Signup and view all the flashcards

JavaScript Statements

Instructions composed of keywords, operators, values and expressions.

Signup and view all the flashcards

Language basics

Syntax, keywords, operators, and built-in objects

Signup and view all the flashcards

JavaScript is loosely typed

Variables are 'loosely typed', they can store any data type without prior declaration.

Signup and view all the flashcards

Using 'let'

Declares variables that can be reassigned different values

Signup and view all the flashcards

Using 'const'

Declares a constant, its value cannot be changed after it's assigned

Signup and view all the flashcards

Anonymous functions

A function assigned to a variable without a name

Signup and view all the flashcards

What are global variables?

Global variables are declared outside a function and are available throughout the code.

Signup and view all the flashcards

Whitespace in Javascript

Whitespace is ignored, use it to improve readability

Signup and view all the flashcards

parseInt()

Converts a string to an integer

Signup and view all the flashcards

External Javascript

Using an external .js file to store javascript code, linked with the src attribute.

Signup and view all the flashcards

Console output

Writing to the browser's Javascript console.

Signup and view all the flashcards

Expression

Producing a value, used in calculations.

Signup and view all the flashcards

Statement

Performing an action, like declaring variables.

Signup and view all the flashcards

Syntax

The rules that govern the Javascript language.

Signup and view all the flashcards

String literals

String literals are text enclosed in single or double quotes.

Signup and view all the flashcards

Symbol

A type property identifier.

Signup and view all the flashcards

Function Return

When Javascript encounters a return statement, the function will stop executing.

Signup and view all the flashcards

Scope

Describes where variables are accessible

Signup and view all the flashcards

Local Scope

Variables that are declared inside a function, only accessible in it's function.

Signup and view all the flashcards

Ternary operator

A shorthand way to evaluate a condition. Consists of a condition, an expression if true, and an expression if false.

Signup and view all the flashcards

let keyword

Variables declared with 'let', can be reassigned new values during code execution.

Signup and view all the flashcards

const keyword

Declare a variable whose value cannot be changed after its initialization.

Signup and view all the flashcards

Writing to HTML

Writing content dynamically into an HTML element using JavaScript, innerText property.

Signup and view all the flashcards

Assignment Operators

Data assigned using =, +=, -=, *=, /=, %= operators. These perform an operation in place on the variable they affect.

Signup and view all the flashcards

JS Data Types

String, Number, Boolean, Object, Function, Symbol, null, and undefined.

Signup and view all the flashcards

Comparison Operators

Ensures equality by comparing value and type while >, <, >= and <=, only compare numerical values.

Signup and view all the flashcards

Study Notes

Basics of JavaScript

  • JavaScript is an object-based scripting language embedded in web browsers such as Chrome, Edge, and Firefox.
  • JavaScript enables client-side scripting, which improves web page responsiveness.
  • Brendan Eich created JavaScript at Netscape in 1995; it was originally named LiveScript.
  • JavaScript was renamed to align with Java's popularity but unrelated to Java.
  • JavaScript allows faster, more interactive, and dynamic web experiences directly in the browser, improving upon the slow response times of server-side scripts.
  • Microsoft introduced JScript, which led to concerns about fragmentation.
  • In 1997, JavaScript was standardized as ECMAScript by Ecma International.
  • Despite standardization, most people refer to it as JavaScript.
  • Brendan Eich co-founded Mozilla and helped launch Firefox.
  • Core capabilities include language basics (syntax, keywords, operators, and built-in objects), web page interaction (DOM manipulation), and web applications (creating responsive web apps and handling JSON data).

Including Scripts

  • JavaScript code can be included directly in an HTML document or placed in external plain text files with a ".js" extension.
  • To include JavaScript code directly in an HTML document, insert it between <script> tags.
  • JavaScript code in an HTML document inline should be inserted between the opening <script> and closing </script> tags.
  • JavaScript can be placed in the <head> section, inside the <body> section, or at the end of the <body> section of an HTML document.
  • Placing JavaScript in the <head> section is not recommended unless it's a small script.
  • Placing JavaScript inside the <body> section can slow down page rendering.
  • The best practice is putting JavaScript at the end of the <body> section to ensure the page loads first before running scripts.
  • To include an external JavaScript file, use the <script> tag with the "src" attribute: <script src="external_script.js"></script>.
  • External JavaScript files promote reusability, better organization, and easier maintenance.
  • Reusability means the same script can be used across multiple web pages.
  • Better organization keeps HTML clean and separates content from behavior.
  • Easier maintenance simplifies updating the .js file updates all linked pages.
  • Do not include <script> tags inside the .js file – only write JavaScript code in the .js file.

Console Output

  • JavaScript can dynamically write content into an HTML element using the innerText property to insert text into the element with the specified ID.
  • JavaScript can display output using a pop-up alert.
  • The alert( ) method of the window object displays content specified within the parentheses in a dialog box.
  • The log( ) method of the console object displays content specified within the parentheses in a console window for debugging.
  • When developing in JavaScript, it is better to display output in the browser's JavaScript console using console.log().

Make Statements

  • JavaScript code is composed of a series of instructions called "statements" executed in top-to-bottom order.
  • Statements comprise keywords, operators, values, and expressions.
  • Keywords are words with special significance in the JavaScript language.
  • Operators are special characters that perform an operation on one or more operands.
  • Values are text strings, numbers, boolean true or false, undefined, and null.
  • Expressions are units of code that produce a single value.
  • In earlier JavaScript, every statement must end with a semicolon (;).
  • Now, semicolons are optional, unless multiple statements are on the same line.
  • Some developers still prefer semicolons for clarity.
  • JavaScript ignores extra spaces, tabs, and line breaks.
  • Spacing improves readability.
  • Use the space bar to indent statements, as tab spacing may be treated differently in text editors.
  • JavaScript statements are often grouped using curly brackets {} inside function blocks.
  • Using curly brackets within function blocks allows for reusable execution when required.
  • Indenting statements by two spaces for better readability is good practice.
  • The rules that govern the JavaScript language are called "syntax."

Values and Variables

  • JavaScript recognizes two types of values: fixed values (literals) and variable values (variables).
  • Number literals can be integer numbers (e.g., 100) or floating-point numbers (e.g., 3.142).
  • String literals are text enclosed in single ('JavaScript Fun') or double ("JavaScript Fun") quotes.
  • Maintain consistency with string literals by using either 'single' or "double" quotes.
  • Variable values, or "variables," store data within a script.
  • Variables are created using the let keyword; for example, let total creates a variable named "total".
  • Assign a value to a variable using the = assignment operator; for example, let total = 300.
  • An expression produces a single value by combining values and operators.
  • Case sensitivity means that JavaScript is case-sensitive, so total and Total are different variables.
  • Comments explain code for readability and debugging.
  • Use single-line comments (//) for short explanations.
  • Use multi-line comments (/* ... */) for longer explanations or disabling blocks of code.
  • Comments explain complex logic.
  • "Comment-out" lines of code to prevent their execution while debugging.

Storing values

  • A "variable" is a container in which data can be stored and retrieved later.
  • JavaScript variables are easier to use because they are "loosely typed, " allowing any type of data to be stored.
  • Strongly typed variables require explicit data type declaration before use.
  • Loosely typed variables do not require data type declaration.
  • JavaScript variables can be declared in four ways: automatically, using var, using let, and using const.
  • Declaring variables automatically involves assigning a value to a variable without explicitly declaring it.
  • An example of automatically declaring a variable x, y and z are undeclared; however, they are declared once there first use in an expression.
  • It is considered better practice to always declare variables before use.
  • The var keyword was used in all Javascript code prior to ECMAScript 2015.
  • The var keyword should only be used in code written for older browsers.
  • The let and const keywords were added to JavaScript in 2015.
  • Variables declared with let can be reassigned new values as the script proceeds.
  • A let variable can be declared without a value and assigned later.
  • Multiple variables may be declared on a single line too.
  • Variables declared with const are constant values and cannot be changed.
  • Use const if the type should not be changed (arrays and objects).
  • Choose meaningful names for variables to make the script easier to read.
  • JavaScript automatically sets the variable type for the value assigned.
  • The variable type can change with subsequent assignments using a different data type to the original variable type.
  • The typeof keyword reveals the current variable type.

Functions

  • A JavaScript function is a block of code designed to perform a particular task and return a final value.
  • JavaScript functions are executed when "something" invokes or calls them.
  • JavaScript functions are defined with the function keyword, followed by a name and parentheses ().
  • Function names can contain letters, digits, underscores, and dollar signs.
  • The parentheses may include parameter names separated by commas.
  • The code to be executed by the function is placed inside curly brackets: {}.
  • Function arguments are the values received by the function when invoked.
  • Inside the function, the arguments (parameters) behave as local variables.
  • Function invocation happens when an event occurs (e.g., a user clicks a button) or when called from JavaScript code.
  • Automatically invoked functions are called self-invoked.
  • When JavaScript reaches a return statement, the function stops executing.
  • If a function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
  • Functions compute a return value that is "returned" back to the "caller."

Assigning Functions

  • Named functions can be assigned to variables.
  • Anonymous functions (or function expressions) can be assigned to variables.
  • Self-invoking functions are used to execute a function automatically when the script loads.
  • Function declarations can be called before they are defined due to hoisting.
  • Function expressions do not support hoisting.

Scope

  • Scope determines where a variable is accessible in a script.
  • JavaScript uses lexical scope, meaning the variable's environment decides its accessibility.
  • Two primary types of scope are global scope and local scope.
  • A global variable is declared outside of any function and is accessible throughout the entire script, but may cause naming conflict issues with external scripts.
  • A local variable is declared inside a function and only exists within that function. It cannot be accessed outside.

Convert Values

  • Conversion is important because JavaScript handles different data types differently.
  • Using mixed data types (e.g., strings and numbers) in operations can lead to unexpected results.
  • Sometimes, it’s necessary to convert a string or other types to numbers perform mathematical operations.
  • Parsing examples include converting the string '42.5px' to the floating-point number 42.5.
  • parseInt() converts a string to an integer, ignoring non-numeric characters after the number.
  • When the conversion fails, JavaScript returns NaN (Not a Number).
  • You can check if a value is not a number using the isNaN(value) function, which returns true if the value is not a number.
  • Numbers can be converted to strings using the String() method or using the toString() method.
  • Example conversions include String(42)'42' and num.toString() where num = 42, the latter returning '42'.

Arithmetic Operators

  • Arithmetical operators commonly used in JavaScript include addition/concatenation, subtraction, multiplication, division, modulus, increment, decrement, and exponentiation.

Assign Values

  • Shorthand Assignment Operators perform an operation and assign the result in one step.
  • The basic assignment operator in JavaScript is the equal (=).
  • Operators include +=, -=, *=, /=, %=, and **=.

Comparison Operators

  • Comparison operators evaluate conditions by comparing two values.
  • The === and !== operators ensure strict equality by checking both value and data type.
  • Relational operators like >, <, >=, and <= compare numerical values. This can lead to type coercion.
  • The ternary operator (?:) is a shorthand way to evaluate a condition.
  • A ternary operator involves three parts: condition ? expression_if_true : expression_if_false.

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser