What is JS ?

At the end of this topic you will:

  • understand basic JS principles
  • to know about functions
  • to know about loops
  • to know about arrays

What is JS?

JavaScript was originally created to "make web pages live." Programs in this language are called scripts. They can be embedded in HTML and executed automatically when the web page is loaded. Scripts are distributed and executed as plain text. They don't need any special preparation or compilation to run. This distinguishes JavaScript from another language, Java.

Today JavaScript can run not only in a browser, but also on a server or any other device that has a special program called a JavaScript "engine". The browser has its own engine, sometimes referred to as the "JavaScript virtual machine".

JavaScript —an object-oriented language with prototypal inheritance. It supports multiple built-in objects and also lets you create or delete your own (custom) objects. Objects can inherit properties directly apart from each other, forming a prototype object chain.

What can JavaScript do in the browser?

Modern JavaScript is a "safe" programming language. It does not provide low-level memory or cpu access, because it was originally built for browsers that do not require it. JavaScript capabilities are highly dependent on the environment, in which he works. For example, Node.JS supports functions for reading / writing arbitrary files, making network requests, etc. Everything related to manipulating web pages is available in the browser for JavaScript, interaction with the user and the web server. For example, in a browser JavaScript can:

  • Add new HTML code to the page, modify existing content, modify styles.
  • React to user actions, mouse clicks, pointer movements, keystrokes.
  • Send network requests to remote servers, download and upload files (AJAX and COMET technologies).
  • Receive and set cookies, ask questions to the visitor, show messages.
  • Recall data on the client side ("local storage")

JavaScript expressions

Expressions in JavaScript are combinations of operands and operators. Operations in expressions are executed sequentially according to the priority value (the higher the priority value, the higher it is). The returned result is not always of the same type as the data being processed. For example, operands of different types are involved in comparison operations, but the returned result will always be of the Boolean type.

Operands —this is data processed by JavaScript. The operands can be both simple data types and complex ones, as well as other expressions.

Operators are symbols in a language that perform various operations on data. Operators can be written using punctuation symbols or keywords.

  • Arithmetic operators
  • Assignment operators
  • Operators of increment and decrement
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • String operators
  • Special operators

LOOPS

JavaScript loops enable repetitive calculations to be performed over and over. They optimize the process of writing code by executing the same instruction or block of instructions that form the body of the loop, a specified number of times (using a counter variable) or while a specified condition is true. Loops traverse a sequence of values. Executing a loop once is called iteration. The loop performance is influenced by the number of iterations and the number of operations performed in the loop body of each iteration.

The following loop operators exist in JavaScript:

  • for is used when you know in advance how many times you need to do something;
  • for ... in is used to traverse the properties of objects;
  • while is used when you don't know how many times to do something;
  • do ... while works similarly to the while statement. The difference is that do ... while always evaluates the expression in curly braces at least once, even if the condition test returns false.
for (var i = 0; i < 5; i++) { console.log(i + ": Hello, JavaScript!"); }

Functions

We often need to repeat the same action in many parts of the program. For example, you need to display a beautiful message when greeting a visitor, when a visitor leaves the site, or elsewhere.

Function declaration

function showMessage() { alert( 'Hello!' ); } let func = (arg1, arg2, ...argN) => expression }

Arrays

Objects allow you to store data with string keys. It is wonderful. But quite often we realize that we need an ordered collection of data, which contains the 1st, 2nd, 3rd elements, etc. For example, we need it to store a list of something: users, products, HTML elements, etc. In this case, using the object is inconvenient because it does not provide methods for controlling the order of elements. We cannot insert a new property "between" existing ones. Objects are simply not designed for these purposes. To store ordered collections, there is a special data structure called an array, Array.

declaration

let arr = new Array(); let arr = [];

let fruits = ["Apple", "Orange", "Plum"]; alert (fruits.length); // 3

Nazar 2 days ago

awesome website. thank you!

0
Reply
Natalia 2 days ago

Amazing!!

0
Reply
Tatsiana 2 days ago

i loved this article!

3
Reply