JavaScript Basics and Features

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 the correct way to include an external JavaScript file?

  • <link rel="script" href="script.js">
  • <script src="script.js"></script> (correct)
  • <script href="script.js"></script>
  • <javascript src="script.js"></javascript>

Which of the following is NOT a component that a JavaScript statement may contain?

  • Operators
  • Values
  • Keywords
  • Objects (correct)

What does the console.log() method primarily do in JavaScript?

  • Displays a pop-up alert box.
  • Executes a function.
  • Displays content in the browser's console. (correct)
  • Writes content to an HTML element.

Which of the following is the correct syntax for a single-line comment in JavaScript?

<p>// This is a comment (C)</p> Signup and view all the answers

Which keyword is used to declare a variable in JavaScript that cannot be reassigned?

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

In JavaScript, what is the primary purpose of a function?

<p>To perform a specific task and optionally return a value. (D)</p> Signup and view all the answers

What is the term used to describe the ability to call function declarations before they appear in the code?

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

What is 'scope' in JavaScript?

<p>The accessibility of variables in a script. (D)</p> Signup and view all the answers

What does parseInt() do in JavaScript?

<p>Converts a value to an integer. (D)</p> Signup and view all the answers

Which operator is used to assign a value to a variable in JavaScript?

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

Which of the following best describes the primary role of JavaScript in web development?

<p>Adding interactivity and dynamic content to webpages. (B)</p> Signup and view all the answers

What is the significance of ECMAScript in relation to JavaScript?

<p>It is the standardized specification upon which JavaScript is based. (A)</p> Signup and view all the answers

Given the following HTML snippet, which placement of the <script> tag is generally recommended for optimal page loading performance?

<p>Immediately before the <code>&lt;/body&gt;</code> tag. (B)</p> Signup and view all the answers

What is the potential drawback of including inline JavaScript code directly within the <body> section of an HTML document?

<p>It can slow down the initial page rendering process. (B)</p> Signup and view all the answers

Which component is NOT part of a JavaScript statement?

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

What is the result of the following JavaScript operation: '10' + 5?

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

Why is it important to use parseInt() or parseFloat() when performing mathematical operations on values obtained from user input fields?

<p>To avoid errors due to incorrect data types. (D)</p> Signup and view all the answers

What will typeof operator return for a variable that has not been assigned a value?

<p>&quot;undefined&quot; (A)</p> Signup and view all the answers

Which of the following statements about JavaScript variable scope is correct?

<p>Variables declared outside of any function have global scope. (D)</p> Signup and view all the answers

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

<p>To specify the value that the function will output. (B)</p> Signup and view all the answers

JavaScript is primarily used for server-side scripting, which impacts web page responsiveness.

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

JavaScript was initially named Mocha and was later renamed to align with Java's popularity, indicating a direct relationship between the two languages.

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

In 1997, JavaScript was standardized as JScript by Ecma International.

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

ECMAScript is the official name for JavaScript.

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

Inline JavaScript code in an HTML document must be enclosed within <javascript> tags.

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

The best practice is to include JavaScript at the end of the <body> section.

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

JavaScript is not case-sensitive, so variables myVariable and MyVariable are treated as the same.

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

In JavaScript, // is used for multi-line comments, while /* */ is used for single-line comments.

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

In JavaScript, a variable declared using const cannot be reassigned a new value after its initial assignment.

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

In JavaScript, String(42) converts the number 42 to a string.

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

JavaScript was originally named ECMAScript and later rebranded due to Java's popularity.

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

In JavaScript, the console.log() method is primarily used for displaying output directly within an HTML element.

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

In JavaScript, keywords are special characters that perform operations on operands.

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

In modern JavaScript, semicolons are always mandatory to terminate statements, regardless of whether multiple statements are on the same line.

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

In JavaScript, String(42) and 42.toString() will both convert the number 42 into a string.

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

In JavaScript, variables declared using let can only store a single, specific data type throughout their entire lifespan.

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

In JavaScript, multiline comments are created using ## at the beginning of each line.

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

A JavaScript function is invoked automatically when the script is loaded, without needing to be called explicitly.

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

In JavaScript, a function declaration can be called before it is defined in the code due to a process called hoisting.

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

In JavaScript, the scope of a variable declared inside a function is global by default.

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

Flashcards

What is JavaScript?

A language embedded in web browsers, enabling client-side scripting and improved web page responsiveness.

Language Basics

Syntax, keywords, operators, and built-in objects that form the foundation of the language.

Web Page Interaction

Manipulating the Document Object Model (DOM) to create dynamic content on web pages.

Web Applications

Creating responsive web applications and handling data in JSON (JavaScript Object Notation) format.

Signup and view all the flashcards

Keywords in JS

Words with special significance in JavaScript, crucial for writing valid code.

Signup and view all the flashcards

Operators in JS

Special characters performing operations on one or more operands, enabling calculations and logic.

Signup and view all the flashcards

Types of values in Javascript

Two types of values recognized by Javascript: Fixed values (literals) and variable values

Signup and view all the flashcards

What is a variable?

A container in every scripting and programming language in which data can be stored and retrieved later.

Signup and view all the flashcards

Variables data type

Variables retain type of initial assigned value. Subsequent assignment can change the data type

Signup and view all the flashcards

Const vs Let

Constants are constant values and cannot be changed, but variables declared with let keyword can be reassigned.

Signup and view all the flashcards

Including Javascript in HTML

In HTML, Javascript code is included between <script> tags.

Signup and view all the flashcards

Whitespace in JavaScript

JavaScript ignores extra spaces, tabs, and line breaks to improve readability.

Signup and view all the flashcards

JavaScript Function

A named block of code performing a task; invoked when 'something' calls it, possibly returning a final single value.

Signup and view all the flashcards

Function Return

When JavaScript reaches a return statement, the function will stop executing.

Signup and view all the flashcards

Scope in JavaScript

Determines a variable's accessibility; JavaScript uses lexical scope, where the environment decides accessibility.

Signup and view all the flashcards

Global Scope Variables

a global variable is declared outside of any function and is accesible throughout the entire script

Signup and view all the flashcards

Local Scope Variables

A local variable is declared inside a function and only exists within that function

Signup and view all the flashcards

parseInt() and parseFloat()

Functions used to convert strings to numeric values

Signup and view all the flashcards

Handling Non-Numeric Values

If the string conversion cannot be done, Javascript returns NaN (Not a Number)

Signup and view all the flashcards

Arithmetic Operators

Basic Assignment (=), Addition (+), Subtraction (-), Multiplication (*), Division(/) and Modulus (%)

Signup and view all the flashcards

ECMAScript

The official name for JavaScript that resulted from standardization efforts.

Signup and view all the flashcards

Console output

Printing or displaying values or messages to the console for debugging and monitoring purposes.

Signup and view all the flashcards

JavaScript Statement

A complete instruction in JavaScript that performs a specific action.

Signup and view all the flashcards

Whitespace

Extra spaces, tabs, and line breaks ignored by JavaScript that serve to improve code readability.

Signup and view all the flashcards

Expression

A unit of code that produces a single value.

Signup and view all the flashcards

Syntax

The rules dictating how JavaScript code must be written.

Signup and view all the flashcards

Automatically declared variables

Variables declared without a declaration keyword.

Signup and view all the flashcards

Numbers to Strings

Numbers converted to text with String() or .toString() methods.

Signup and view all the flashcards

Functions to Variables

Functions assigned directly to variables (anonymous) or named functions assigned to variables

Signup and view all the flashcards

Self-Invoking Function

A function that is executed as soon as it is defined.

Signup and view all the flashcards

Sequential Execution

JavaScript uses this to process code, progressing sequentially from top to bottom.

Signup and view all the flashcards

Readable Code

The practice of using spaces and indentation to enhance code readability in JavaScript.

Signup and view all the flashcards

JavaScript Statement Components

A fundamental component of JavaScript code, containing keywords, operators, values, and expressions.

Signup and view all the flashcards

Keywords

Reserved words with special meanings in JavaScript, like 'var', 'let', 'const', and 'function'.

Signup and view all the flashcards

LiveScript

The original name of JavaScript when it was first created by Brendan Eich at Netscape.

Signup and view all the flashcards

Case Sensitivity

A coding error that occurs when JavaScript interprets upper and lower case letters differently in a variable name.

Signup and view all the flashcards

Comments

Adding human-readable explanations within Javascript code to further communicate the code's purpose.

Signup and view all the flashcards

Var Declaration

Declaring Javascript variables using this keyword is for older browsers only (1995-2015).

Signup and view all the flashcards

Const Declaration

Declaring Javascript variables using this keyword makes them immutable or unchangeable.

Signup and view all the flashcards

Ternary operator

A shorthand way to evaluate a condition in JavaScript.

Signup and view all the flashcards

Study Notes

Basics of JavaScript

  • JavaScript is an object-based scripting language for web browsers like Chrome, Edge, and Firefox.
  • Client-side scripting with JavaScript enhances webpage responsiveness.
  • Brendan Eich created JavaScript at Netscape in 1995, originally named LiveScript.
  • JavaScript was renamed to align with Java’s popularity, but Java and JavaScript are unrelated.
  • Before JavaScript, web pages used server-side scripts, leading to slow response times.
  • JavaScript enables faster, more interactive, and dynamic web experiences directly in the browser.

JavaScript Evolution & Key Features

  • Microsoft's JScript introduction led to standardization concerns.
  • In 1997, JavaScript was standardized as ECMAScript by Ecma International.
  • JavaScript is still the popular name despite the ECMAScript standardization.
  • Brendan Eich co-founded Mozilla and launched Firefox.
  • Core capabilities in JavaScript include: syntax, keywords, operators, built-in objects, DOM manipulation for dynamic content,- responsive web app creation, and JSON data handling.

Including Scripts in HTML

  • JavaScript code can be embedded directly within an HTML document using <script> tags.
  • JavaScript can reside in the <head>, <body>, or at the end of the <body> section of an HTML document.
  • Placing scripts at the end of the <body> ensures the page loads before the scripts execute. This is considered a best practice.
  • JavaScript code included directly in to an HTML document it must be inserted between <script> tags.
  • External JavaScript files use a .js extension.
  • Use the <script> tag with the src attribute to insert external JavaScript files.
  • External JavaScript files promote reusability across multiple web pages.
  • They improve organization by separating content from behavior.
  • External files allow for easier maintenance because updating the .js file updates all linked pages.
  • Do not include <script> tags inside .js files.

Console Output

  • JavaScript dynamically writes content into HTML elements 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 is used on the window object to display content in a dialog box.
  • The console.log() method displays content in the browser's JavaScript console, useful for debugging and learning.

JavaScript Statements

  • JavaScript code consists of statements executed in top-to-bottom order.
  • Statements can include keywords, operators, values, and expressions.
  • In earlier JavaScript versions, semicolons (;) were needed to terminate statements.
  • Now, semicolons are generally optional unless multiple statements are on the same line.
  • JavaScript ignores extra spaces, tabs, and line breaks.
  • Use spacing to improve code readability.
  • Indent statements using the space bar for readability.
  • Expressions produce a value, while statements perform an action.
  • The rules for JavaScript language are called "syntax."
  • JavaScript statements are grouped using curly brackets {} in function blocks.
  • Indent statements by two spaces for better code readability.

JavaScript Values

  • JavaScript recognizes two types of values: fixed values (literals) and variable values (variables).
  • Number literals can be integers (e.g., 100) or floating-point numbers (e.g., 3.142).
  • String literals are text enclosed in single (e.g., 'JavaScript Fun') or double (e.g., "JavaScript Fun") quotes.
  • Be consistent in the use of single or double quotes for string literals.
  • Variables store data within a script and are created using the let keyword.
  • A variable can be assigned to a value using the = assignment operator.

Expressions & Operators

  • An expression combines values and operators to produce a single value.
  • The let keyword can be used to assign an expression with operators.
  • JavaScript is case-sensitive.

Comments

  • Comments explain code for readability and debugging.
  • Single-line comments use // for short explanations.
  • Multi-line comments use /* ... */ for longer explanations or disabling code blocks.

Variables

  • A variable is a container for storing and retrieving data in scripting and programming languages.
  • JavaScript variables are easier because they are loosely typed, any type of data.
  • Strongly typed variables require declaring a specific data type before use.
  • Loosely typed variables can store any data type without prior declaration.
  • Variables can be declared automatically, using var, let, or const.
  • Declaring variables before use is good programming practice.
  • When a variable is declared automatically the variables are undeclared and it's good practice to always declare.
  • The var keyword was used in all JavaScript code from 1995 to 2015 and is primarily used in older browsers.
  • The let and const keywords were added to JavaScript in 2015.
  • Variables declared with let can be reassigned new values and variables can be declared without a value and assigned later with let, multiple variables can be declared on a single line.
  • The const keyword is used for constant values that cannot be changed when the type shouldn't be altered, such as Arrays and Objects.
  • Choose meaningful variable names.
  • In JavaScript, the value assigned to a variable determines its type and assignation of a different data type later can be made to change the variable type.
  • The typeof keyword reveals the current variable type.

Functions

  • A JavaScript function is a block of code designed to perform a specific task, and it returns a final single value.
  • An event, such as a user clicking a button, is an example how a function is executed.
  • Execution occurs when invoked, and function return specifies the value returned.
  • Function names can contain letters, digits, underscores, and dollar signs.
  • The parentheses, when making a function, may include parameter names separated by commas: (parameter1, parameter2,...
  • When JavaScript reaches a return statement, the function will stop executing.
  • When invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
  • Functions can compute a return value so the return value is “returned” back to the "caller".
  • Function arguments are the values received by the function when invoked and behave as local variables inside the function.
  • Assigning functions to variables includes assigning a named function to a variable, anonymous functions (function expressions, and self-invoking functions.
  • Self-invoking functions are used widely to execute example code when the script gets loaded.
  • JavaScript reads scripts in two sweeps finding function declarations and 'hoists' them to the top.
  • Function declarations can be called before defined, if the function is hoisted.
  • Function expressions cannot be hoisted.

Function Scope

  • Scope determines variable accessibility in a script.
  • JavaScript uses lexical scope, meaning the variable's environment determines accessibility.
  • The main two types of scope are global and local.
  • Global variables are declared outside of any function and are accessible throughout the script.
  • A potential issue of global scopes is naming conflicts with external scripts
  • Local variables are declared inside of a function.
  • Local variables existing only within that function, cannot be accessed outside.

Value Conversion

  • JavaScript handles different data types differently and may converts them for mathematical operations.
  • Strings can be converted using built-in functions like parseInt() and parseFloat().
  • parseInt() converts strings to integers, while parseFloat() converts strings to floating-point numbers.
  • Conversions will give Nan if a non number is returned
  • The isNaN() function returns true if the value is not a number.
  • Numbers can be turned into strings via String(<number>), or <number>.toString().

Arithmetic

  • Arithmetical operations include addition, subtraction, multiplication, division, modulus(%), increment, decrement and exponentiation.

Assignment

  • The = operator assigns values
  • Shorthand operators perform an operation and assign the result in a single step +=, -=, *=, |=, %=, **=

Comparisons

  • Comparison operators evaluate conditions by comparing values.
  • === and !== check both value and data type for strict equality and inequality.
  • Relational operators like >, <, >=, and <= compare numerical values.
  • Greater than > returns true if the first operand is greater.
  • Less than < returns true if the first operand is smaller.
  • Greater or equal to >= Returns true if the first operand is greater or equal.
  • Less than or equal to <= Returns true if the first operand is smaller or equal.
  • There are only true or false for either checking equality or inequality.

Examine Conditions

  • Ternary operator (?:) is a shorthand way to evaluate a condition in 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