Java Sp Crit
Java Sp Crit
Introduction to JavaScript
This study guide covers the fundamentals of JavaScript, including its definition,
capabilities, execution environments, and relationship to ECMAScript.
What is JavaScript?
JavaScript is a popular and widely used programming language.
It's one of the fastest-growing languages, used by major companies like Netflix,
Walmart, and PayPal.
The average salary of a JavaScript developer in the United States is $72,000
per year.
Roles include:
Front-end developer
Back-end developer
Full-stack developer
Page 1
Created by Turbolearn AI
console.log('Hello world');
alert('Yo');
Code Editors
Page 2
Created by Turbolearn AI
Node.js
Download Node.js from nodejs.org.
While not strictly necessary for executing JavaScript (as browsers can do this),
Node.js is useful for:
Installing third-party libraries.
Backend development.
Project Setup
1. Create a new folder (e.g., js-basics).
2. Open the folder in Visual Studio Code.
3. Add a new file: index.html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Basics</title>
</head>
<body>
<h1>Hello World</h1>
<script>
console.log('Hello world');
</script>
</body>
</html>
Page 3
Created by Turbolearn AI
<h1>Hello World</h1>
Save the changes, and the browser should automatically refresh, displaying "Hello
World."
Script Element
JavaScript code is placed inside <script> elements.
The best practice is to put the <script> element at the end of the <body>
section.
This ensures that all HTML elements are rendered before the JavaScript
code runs.
Avoids blocking page rendering while the browser parses and executes
JavaScript.
Ensures that JavaScript code can interact with all elements on the page.
Statements
A statement is a piece of code that expresses an action to be carried out.
All statements in JavaScript should be terminated by a semicolon.
Strings
Page 4
Created by Turbolearn AI
Comments
Comments are used to add descriptions to code and are ignored by the
JavaScript engine.
Example:
Page 5
Created by Turbolearn AI
2. Move the JavaScript code from the <script> element in your HTML file to the
new .js file.
3. Reference the .js file in your HTML using the src attribute in the <script> tag:
<script src="index.js"></script>
3. Run the Code: Execute the JavaScript file using the command:
node index.js
Page 6
Created by Turbolearn AI
Variables are used to store data temporarily in a computer's memory. They are like
boxes with labels, where the label is the variable name, and the contents of the box
are the value assigned to the variable.
Cannot be a
Variables cannot be named after JavaScript reserved Invalid: let
reserved
keywords like let, if, else, or var. if;
keyword
Should be Variable names should clearly indicate the purpose of Good:
meaningful the variable. firstName;
Cannot start with Invalid:
Variable names cannot begin with a numeric character.
a number 1name;
Use camel case notation for multiple words. The first
Cannot contain a letter of the first word should be lowercase, and the Good:
space or hyphen first letter of each subsequent word should be firstName;
uppercase.
JavaScript variable names are case-sensitive, meaning
Case-sensitive name != Name
name and Name are treated as different variables.
Page 7
Created by Turbolearn AI
Camel Case Notation: Writing variable names where the first word is
lowercase, and each subsequent word starts with a capital letter.
Example:
Page 8
Created by Turbolearn AI
```js
let firstName;
let lastName;
```
## Constants
```js
const interestRate = 0.3;
interestRate = 1; // This will cause an error
```
## Data Types
```js
let name = 'Mosh'; // String literal
```
* **Number:** Numeric values.
```js
let age = 30; // Number literal
```
* **Boolean:** `true` or `false`.
```js
let isApproved = true; // Boolean literal
```
* **Undefined:** Represents a variable that has not been assigned a valu
```js
let firstName; // Value is undefined by default
let lastName = undefined; //Explicitly assigned undefined
```
* **Null:** Represents the intentional absence of a value.
Page 9
Created by Turbolearn AI
```js
let selectedColor = null; // Used to clear the value of a variable
```
## Dynamic Typing
```js
let name = 'Mosh'; // Type is string
name = 30; // Type is now number
You can use the typeof operator to check the type of a variable:
typeof name;
Even if you change a number to a floating-point number, its type remains number:
Reference Types
There are three key reference types:
Objects
Arrays
Functions
Page 10
Created by Turbolearn AI
Objects
An object in JavaScript is a collection of key-value pairs, representing
properties and methods.
When dealing with multiple related variables, you can group them inside an object.
Instead of declaring separate variables, you can declare a single person object and
reference its properties.
Object Literal: Syntax using curly braces {} to define an object. Inside the
braces, you add one or more key-value pairs, where keys are the
properties of the object.
Example:
let person = {
name: "Maash",
age: 30
};
console.log(person);
Dot Notation
Bracket Notation
Dot Notation
The dot notation is a way to access an object's properties by using a dot (.) followed
by the property name.
Example:
Page 11
Created by Turbolearn AI
person.name = "John";
console.log(person.name); // Output: John
Bracket Notation
The bracket notation uses square brackets [] with a string inside to specify the
property name.
Example:
person["name"] = "Mary";
console.log(person["name"]); // Output: Mary
Example:
Arrays in JavaScript
Arrays are used to store lists of objects.
Page 12
Created by Turbolearn AI
Example:
Example:
Example:
selectedColors[2] = 'green';
selectedColors[3] = 10;
console.log(selectedColors); // Output: ['red', 'blue', 'green', 10]
Arrays as Objects
Arrays in JavaScript are technically objects. Each array has a set of key-value pairs or
properties that can be accessed using dot notation.
Page 13
Created by Turbolearn AI
Example:
Length Property
The length property of an array returns the number of elements in the array.
Functions in JavaScript
Function: A set of statements that performs a task or calculates a value.
Example:
function greet() {
console.log('hello world');
}
Function Parameters
Functions can have inputs called parameters, which can change the behavior of the
function.
Example:
Page 14
Created by Turbolearn AI
function greet(name) {
console.log('Hello ' + name);
}
```## Functions in JavaScript
When defining a function, you can specify **parameters**, which act as pla
For example:
```js
function greet(name) {
console.log("Hello " + name);
}
In the greet function, name is a parameter. It is only meaningful inside this function,
and is not accessible outside of it.
When you call a function, you provide arguments, which are the actual values that
are passed to the function's parameters.
For example:
greet("John");
Page 15
Created by Turbolearn AI
If you don't provide an argument for a parameter, its value defaults to undefined.
Performing a Task
A function that performs a task typically executes a series of statements to achieve a
specific outcome, such as displaying something on the console.
Example:
function greet(name) {
console.log("Hello " + name);
}
Calculating a Value
A function that calculates a value performs a computation and returns the result. The
return keyword is used to send the calculated value back to the caller.
Example:
function square(number) {
return number * number;
}
In this case, the square function calculates the square of a number and returns the
result.
Function Calls
Page 16
Created by Turbolearn AI
When you use a function in javascript, there will always be a function call. Even
something that looks simple, like:
console.log("Hello");
Is a function call. Here, we are calling the log function which is defined somewhere
else, and passing it the argument "Hello". Similarly:
console.log(square(2));
Real-World Applications
A real-world application is essentially a collection of hundreds or thousands of
functions working together to provide the functionality of that application.
Page 17