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

JavaScript is known as what type of language?

  • Machine
  • Assembly
  • Low-level
  • Object-based scripting (correct)

In what year was JavaScript created?

  • 2000
  • 1990
  • 1995 (correct)
  • 2005

What was JavaScript originally named?

  • LiveScript (correct)
  • HyperScript
  • JScript
  • ECMAScript

What is the primary function of client-side scripting?

<p>Improving web page responsiveness (B)</p> Signup and view all the answers

What organization standardized JavaScript?

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

Which of these is a core capability of JavaScript?

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

What is the correct way to include inline JavaScript code within an HTML document?

&lt;script> // JavaScript code &lt;/script> (D) Signup and view all the answers

What is the recommended location for placing JavaScript code in an HTML document?

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

To use an external JavaScript file, which HTML tag is used?

&lt;script> (A) Signup and view all the answers

What extension should external JavaScript files have?

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

Which property is used to insert text into an HTML element?

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

How is output displayed using a pop-up alert in JavaScript?

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

What is a 'statement' in JavaScript?

<p>A single instruction (B)</p> Signup and view all the answers

What is the term for words that have special significance in the JavaScript language?

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

What are the two types of values that JavaScript recognizes?

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

Using which keyword can you create a JavaScript variable?

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

What is the purpose of comments in JavaScript?

<p>To explain code (D)</p> Signup and view all the answers

What is the term for the rules that govern the JavaScript language?

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

What will parseInt('42.5abc') return in JavaScript?

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

What is the purpose of the isNaN() function in JavaScript?

<p>To check if a value is not a number (C)</p> Signup and view all the answers

JavaScript is not an object-based scripting language.

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

JavaScript was created by Brendan Eich at Netscape in 1995.

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

JavaScript is directly related to Java.

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

All web pages relied on client-side scripts before JavaScript.

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

JavaScript was standardized as ECMAScript in 1997.

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

External JavaScript files are included using the <link> tag.

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

JavaScript ignores tabs and spaces.

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

JavaScript is not case-sensitive.

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

Comments in JavaScript do not improve code readability.

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

A variable is a container for data that can be stored and retrieved.

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

JavaScript variables must be declared with a specific type, like Java.

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

The const keyword is used for values that can be changed.

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

The typeof keyword reveals the current variable type.

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

A JavaScript function is not executed when something invokes (calls) it.

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

Function names can only contain letters.

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

Function arguments do not behave as local variables.

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

A JavaScript function stops executing when it reaches a return statement.

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

Lexical scope means a variable's accessibility is decided by its value.

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

Global variables are accessible throughout the entire script.

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

Local variables can be accessed outside the function in which they are declared.

<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.

Language Basics

Syntax, keywords, operators, and built-in objects form the foundation of this type of code.

Web Page Interaction

Manipulating the DOM (Document Object Model) to create dynamic and interactive content.

Web Applications

Creating responsive web applications capable of handling JSON data for dynamic content.

Signup and view all the flashcards

Inline JavaScript

Inserting JavaScript code directly within an HTML document using the tags.

Signup and view all the flashcards

External JavaScript

Writing JavaScript code in external plain text files with a .js extension.

Signup and view all the flashcards

JavaScript Statements

A series of instructions specifying actions for the computer to perform, forming the basis of code execution.

Signup and view all the flashcards

Keywords

Words with special significance that form the foundation of JavaScript's structure which performs specific operations.

Signup and view all the flashcards

Expressions

A unit of code that can be evaluated to produce a value, such as a variable, operator, or function call.

Signup and view all the flashcards

Fixed Value (Literal)

A value that does not change at runtime. Can be any of the primitive data types.

Signup and view all the flashcards

Variable Values

A named storage location that can hold varying types of data during the execution of the code.

Signup and view all the flashcards

Using var

The var keyword should only be used in the code written for older browsers, not new code.

Signup and view all the flashcards

Using let

The let and const keywords were added to JavaScript in 2015. Variables declared with let can be reassigned new values.

Signup and view all the flashcards

Using const

Constant values and cannot be changed in (Arrays and Objects).

Signup and view all the flashcards

What is a function?

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

Signup and view all the flashcards

Function Hoisting

Functions read and execute scripts in two sweeps: Finds function declarations and hoists them to the top, executes the script.

Signup and view all the flashcards

What is Scope?

Determines where a variable is accessible in a script. JavaScript uses lexical scope, meaning the variable's environment decides its accessibility.

Signup and view all the flashcards

Strings to Numbers

Used to converts to numbers using built-in functions like parseInt() and parseFloat().

Signup and view all the flashcards

Non-Numeric Values

If the conversion fails return NaN (Not a Number).

Signup and view all the flashcards

Comparison operators

Comparison operators are used to evaluate conditions by comparing two values.

Signup and view all the flashcards

JavaScript standardization

Standardized as ECMAScript by Ecma International in 1997

Signup and view all the flashcards

Writing to an HTML Element

Writing to an HTML element using JavaScript

Signup and view all the flashcards

Output Using an Alert

Displaying output using a pop-up alert in JavaScript

Signup and view all the flashcards

Console Output for Debugging

Displaying output in the browser's JavaScript console for debugging.

Signup and view all the flashcards

Keywords in JavaScript

Words that have special meaning and perform specific actions.

Signup and view all the flashcards

Whitespace in JavaScript

JavaScript ignores extra spaces, tabs, and line breaks improving code appearance.

Signup and view all the flashcards

Grouping Statements with Braces

Grouping statements using curly brackets {} to define code blocks.

Signup and view all the flashcards

Dynamic Typing

JavaScript automatically determines the variable's data type based on the assigned value.

Signup and view all the flashcards

Strongly Typed Variables

These must declare a specific data type before use

Signup and view all the flashcards

Loosely Typed Variables

This can store any data type without prior declaration

Signup and view all the flashcards

Global Scope

Declared outside any function, accessing them throughout the entire script.

Signup and view all the flashcards

Local Scope

Declared inside a function, accessible only within that function.

Signup and view all the flashcards

Arithmetic Operators

Arithmetical operators perform calculations and are commonly used in JavaScript (+, -, *, /, etc.).

Signup and view all the flashcards

Assignment Operator (=)

Basic assignment operator (=) assigns a value to a variable.

Signup and view all the flashcards

Shorthand Assignment Operators

The shorthand operators perform an operation and assign the result in one step (+=, -=, *=, /=).

Signup and view all the flashcards

Ternary Operator (?:)

A concise way to evaluate a condition and execute one of two expressions.

Signup and view all the flashcards

Study Notes

Basics of JavaScript

  • JavaScript is an object-based scripting language embedded in web browsers like Chrome, Edge, and Firefox
  • Client-side scripting is enabled, which improves web page responsiveness
  • Brendan Eich created JavaScript at Netscape in 1995, originally named LiveScript
  • JavaScript was renamed to align with Java's popularity but has no relation to Java
  • JavaScript allows faster, interactive, and dynamic web experiences directly in the browser, unlike slow server-side scripts

JavaScript Evolution & Key Features

  • Microsoft introduced JScript, leading to concerns about fragmentation
  • In 1997, JavaScript was standardized as ECMAScript by Ecma International
  • Most still call the language JavaScript despite the official name
  • Brendan Eich co-founded Mozilla and launched Firefox
  • Core capabilities include language basics with syntax, keywords, operators, and built-in objects
  • Web page interaction involves manipulating the DOM for dynamic content
  • Web applications involve creating responsive web apps and handling JSON data

Including Scripts

  • JavaScript code can be included directly in an HTML document using the <script> tag
  • JavaScript can be placed in the <head>, <body>, or at the end of the <body>
  • Putting the script at the end of the <body> tag is best, which ensures the page loads before running scripts
  • JavaScript code can also be written in external .js files
  • External JavaScript files can be included using the <script> tag with the src attribute
  • Reusability is increased when using external Javascript files
  • Better organization is achieved separating HTML and JavaScript
  • Easier maintenance when you update the .js file updates all linked pages
  • Internal Javascript files shouldn't include <script> tags the files should contain the raw javascript code.

Console Output

  • Javascript can dynamically write content to an HTML element
  • The innerText property inserts text into the HTML element with the id specified
  • Javascript can display output through a popup alert.
  • The alert() method displays content specified within parentheses in a dialog box
  • The console.log() method displays content within parentheses in the browser's JavaScript console

Statements

  • Javascript code is made up of statements, which are excuted in a top-to-bottom order
  • Each statement consists of keywords, operators, values, and expressions
  • In earlier Javascript, every statement needed to end with a semi-colon
  • However, it has since changed so semi-colons are now optional, unless multiple statements are on a single line
  • Whitespace is ignored by JavaScript
  • Use the space bar to indent statements
  • Expressions produce a value while statements perform an action

JavaScript Statements & Syntax

  • JavaScript has syntax, which are the rules that govern the JavaScript Language
  • JavaScript statements are grouped using curly brackets inside of functional blocks
  • These blocks are for reusable execution
  • Indent statements by two spaces for better readability

Variable Values

  • There are two kinds of Javascript values, Fixed Values (Literals) and Variable Values (Variables)
  • There are two kinds of literals, Number literals and String literals
  • Integer numbers (100) or floating-point numbers (3.142) are examples of number literals
  • "Javascript fun" wrapped in single or double quotes are string literals
  • Variables are created using the Javascript let keyword
  • let total can be assigned a value of 300 using the = assignment operator

Expressions and Operators

  • Expressions are an expression produce a single value with its values and operators
  • Numeric Expressions: let result = (*80 + 20)
  • Operations with variables: let newTotal = (total - 200)
  • Operators are used in expressins: let finalValues = ( 100 + 50) * 2

Case Sensitivity

  • Javascript is case sensitive
  • Variable names like Total and total are different

Comments

  • Javascript has single and multi line comments to help with readability and debugging
  • Single line comments are //
  • Multi line comments /* */
  • Comments should be used to explain complex logic or exclude lines of code while debugging

Variables

  • Variables are a container to store and retreive data
  • Javascript variables are loosely typed, meaning they can contain any data type

Variable Typing

  • In Javascript, you don't need to predefine a variable's data type
  • In other languages like Java, you need to predefine the data type
  • Example of a strongly typed variable: int age = 25
  • Example of a loosely typed variable: let data = 25

Variable Declaration

  • Javascript has 4 ways to declare variables:
  • Using the keyword automatically
  • Using the keyword "var"
  • Using the keyword "let"
  • Using the keyword "const"
  • In 1, with automatically, if variables aren't explicitly defined, they are automatically declared when they are first used
  • Variables can be declared on a single line: let i, j, k
  • The let and const keywords were added to Javascript in 2015
  • Const means a constant value and cannot be changed
  • Use const if the type should not be changed
  • Javascript selects the variable type for you, but you can also use a typeof Javascript keyword

Functions

  • A Javascript Function is a block of code designed to return a particular task
  • Functions are executed when something calls them

Javascript Function Syntax

  • Javascript functions are defined with the function key word
  • It should be followed by a name, followed by 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 will be placed inside curly brackets: {}
  • Function arguments are the values received by the function when it is invoked
  • Insert the function, the argument is called local variables

Invoking Functions

  • Function can be invoked in an event, when it is called, or automatically invoked
  • When Javascript reaches a return statement, the function will stop executing
  • if the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
  • Functions return to the "caller"
  • Functions can also compute values

Assigning Functions to Variables

  • Functions can be assigned to variables
  • This can be anonymous for function expressions
  • Can be self invoking

Function Hoisting

  • Javascript reads scripts in two sweeps, first finds function declarations then executes
  • Function declarations can be called before they are defined
  • But function expressions are not hoisted

Recognize Scope

  • Scope is where a variable can be accessed in a script
  • Javascript uses lexical scope, meaning a variable's environment defines scope
  • Main types of scope include global and local scope

Global Scope

  • A global variable is declared outside of any function, accessible through the entire script
  • Potential issue include naming conflicts with external scripts

Local Scope

  • A local variable is declared inside a function
  • It only exists within that function and cannot be accessed outside

Convert Values

  • Different data types are handled differently
  • Using mixed data types lead to unexpected results

Strings to Numbers

  • Strings can be converted to numbers using a built-in function parseInt() or parseFloat()
  • These functions allow string values to work with number types
  • parseInt('42') converts to 42, an integer whole number
  • parseInt('42nd Street') converts to 42, ignoring alphabetic characters
  • parseFloat converts a string to a floating point number
  • parseFloat('42.5px') converts to 42.5
  • parseFloat('42.5abc') converts to 42.5, ignoring alphabetic characters
  • If the conversion fails, Javascript returns NaN
  • An example is parseInt('Hello") converts to NaN
  • To check, you can use isNaN(value) which returns true if the value is not a number

Numbers to Strings in Javascript

  • Use the String() method to converts a number to a string.
  • Example: String(42) becomes '42'
  • Use the toString() method to converts a number to a string.
let num = 42;
num.toString(); // Returns '42'

Do Arithmatic

  • Arithmatic Operators are Addition, Subtraction, Multiplication, Division, Modulus, Increment, Decrement, Exponentiation, etc

Assign Value Operators

  • Use the = for assignment of values
  • Use =+ for addition
  • Use -+ for subtraction
  • Use /+ for division
  • Use % for modulus
  • Use ** for exponentiation

Make Comparisons

  • Javascript uses comparison operators to evaluate two values
  • The "===" and "!==" operators evaluate strict equality and type
  • Relation operators like "greater than", "less than", or "equal to" also works

Equality and Inequality Operators

  • Equality (===) means true if both value and type are identical.
  • Example: 25 === '25' result is false because of the different data types
  • Inequality (!==) means Returns true if value or type are different.
  • Example: 25 !== '25' result is true
  • > Returns true if the first operand is greater.
  • < Returns true if the first operand is smaller.
  • >= Returns true if the first operand is greater or equal.
  • The == and != operators allow type coercion, which can lead to unexpected results. It is better to use === and !== to be more accurate

Conditions

  • One Javascript keyword is the ternary operator shorthand
  • It has 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