JavaScript with Node.js

EnthusiasticRisingAction avatar
EnthusiasticRisingAction
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the output of the following code: let student = {name: 'John', age: 20, major: 'CS'}; for (let prop in student) { console.log(prop + ': ' + student[prop]); }

name: John, age: 20, major: CS

What is the purpose of the while loop in the following code: let i = 0; while (i < 5) { console.log(i); i++; }

To execute a block of code repeatedly until a condition is met

What is the difference between a function declaration and a function expression?

A function declaration is used to define a function, while a function expression is used to define a function as a value.

What is the purpose of the rest parameter syntax in JavaScript?

To define a function with a variable number of arguments

What is the output of the following code: let greet = function(name) { return 'Hello, ' + name; }; console.log(greet('John'));

Hello, John

What is the difference between let and const in JavaScript?

let is used to declare a variable, while const is used to declare a constant

What is the purpose of the for...in loop in the following code: let student = {name: 'John', age: 20, major: 'CS'}; for (let prop in student) { console.log(prop + ': ' + student[prop]); }

To iterate over the properties of an object

What is the output of the following code: let i = 0; do { console.log(i); i++; } while (i < 5);

0, 1, 2, 3, 4

What is the purpose of arrow functions in JavaScript?

To define a function with a shorter syntax

What is the difference between console.log() and return in JavaScript?

console.log() is used to log a message to the console, while return is used to exit a function

Study Notes

Node.js and JavaScript Basics

  • Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, allowing JavaScript to run on servers or laptops.
  • Node.js can be installed from https://nodejs.org/.
  • Running Node.js interactive shell: open terminal, type node, and press Enter.
  • JavaScript code can be entered in the interactive shell and executed immediately.

JavaScript Console Example

  • Example of using JavaScript console:
    • Declare a variable: let x = 10;
    • Log the variable: console.log(x);
    • Perform arithmetic operations: x + 5;
    • Declare a string variable: let y = 'Hello, world!';
    • Convert string to uppercase: y.toUpperCase();

JavaScript Basics

  • JavaScript has only seven primitive types, which are not objects and have no methods:
    • Undefined (a variable that has not been assigned a value)
    • Null (represents the intentional absence of any object value)

Required Reading

  • Required reading for JavaScript: "JavaScript Crash Course" by Nick Morgan, "Part I: The Language"

JavaScript, HTML, and CSS

  • JavaScript:
    • Is an object-oriented programming language
    • Runs in web browsers
    • Can be used to design/program web page behavior
  • HTML (Hyper Text Markup Language):
    • Standard markup language for creating web pages
    • Describes web page structure
  • CSS (Cascading Style Sheets):
    • Style sheet language for describing web page look and formatting
    • Provides visual aspects of web pages

Browser Consoles

  • Browser consoles allow writing and executing JavaScript code on the fly
  • Example of using browser console:
    • Declare an object: let student = {name: 'John', age: 20, major: 'CS'};
    • Use for...in loop to iterate over object properties

Conditionals and Loops

  • Examples of while and do-while loops:
    • While loop: while (i < 5) { console.log(i); i++; }
    • Do-while loop: do { console.log(j); j++; } while (j < 15);

Functions

  • Functions can be defined:
    • Using function declarations: function greet1(name) { ... }
    • Using function expressions: const greet2 = function(name) { ... }
    • Using arrow functions: const greet3 = (name) => { ... }
  • Rest parameter syntax allows functions to accept any number of arguments as an array.

Learn about using Node.js to run JavaScript on your server or laptop. Discover how to install Node.js and start the interactive shell.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser