Javascript Fundamentals
Alerts and Prompts
Using JS, we can display alerts and prompts on the screen. Here are the four ways to do so:
- console: We can display messages in the browser console using the console.log() method.
- dialog box: We can display alerts and prompts using dialog boxes using methods like alert(), confirm(), and prompt().
- writing HTML element: We can write to an HTML element using its ID using the getElementById() method.
- writing to the browser window: We can write directly to the browser window using the document.write() method.
Variables
In JavaScript, we declare variables using the var keyword. Here are some rules to follow when naming variables:
- Variable names can only contain letters, digits, underscores, and dollar signs.
- Variable names cannot start with a digit.
- Variable names are case-sensitive.
- It is a good practice to use descriptive and meaningful names for your variables.
Some data types in JavaScript include numbers (integers and floats), strings, and arrays.
Arrays
Arrays are a data structure in JavaScript that can hold multiple values in a single variable. Here are some basic operations we can perform with arrays:
- We can create an array using the var keyword and assigning values to it inside square brackets.
- We can access the values of an array using its index using square brackets.
- We can add elements to an array using the push() method.
- We can remove the last element of an array using the pop() method.
- We can remove the first element of an array using the shift() method.
- We can get the length of an array using the length property.
- We can loop through an array using for and forEach loops.
- We can join elements of an array into a string using the join() method.
- We can split a string into an array using the split() method.
- We can concatenate multiple arrays using the concat() method.
- We can get the index of an element in an array using the indexOf() method.
- We can get the last index of an element in an array using the lastIndexOf() method.
Functions
Functions are blocks of code that perform a specific task. We can define functions using the function keyword. Here are some concepts related to functions:
- We can call a function using its name followed by parentheses.
- We can pass arguments to a function inside the parentheses.
- A function can return a value using the return keyword.
- Variables declared inside a function are only accessible within the function (they have a local scope).
- Functions can be defined as function declarations or function expressions.
- Arrow functions are a newer syntax for defining functions that have a more concise syntax and lexically bind the this keyword.
- Functions can also be used as arguments to other functions, which is known as higher-order functions.
Objects
Objects are a complex data type in JavaScript that can hold multiple values and functions. Here are some basic concepts related to objects:
- Objects are defined using curly braces {} and can contain properties and methods.
- Properties are key-value pairs that represent the state of an object.
- Methods are functions that can be called on an object to perform specific tasks.
- We can access object properties using dot notation or bracket notation.
- We can add or modify object properties using dot notation or bracket notation.
- We can delete object properties using the delete keyword.
- We can check if an object has a property using the hasOwnProperty() method.
- We can get all the keys or values of an object using the Object.keys() or Object.values() method, respectively.
- We can create objects using constructor functions or classes.
- JavaScript also has some built-in objects like Math and Date.
0 Comments
We love hearing from our viewers! Your comment is important to us. ❤️