Programming

JavaScript

Test · Nibras DevAfter this page, you can try the code in Nibras Dev.

What is JavaScript?

JavaScript is a widely used programming language for building modern websites and web applications. Together with HTML and CSS, it is one of the main parts of web technology. JavaScript can add interaction to a page and can also be used to build different kinds of applications.

It is mainly used to make web pages interactive and dynamic. It used to be seen mostly in the browser. Today it is also used on the server and in other application environments.

With JavaScript you can react to buttons, handle forms, run animations, change what is on the page, and respond to the user.

What is JavaScript used for?

JavaScript is used in several areas:

  • Building websites
  • Interactive web applications
  • Frontend programming
  • Backend programming
  • Working with APIs
  • Mobile applications
  • Desktop applications
  • Games and interactive projects
  • Handling forms and user input

Through technologies such as Node.js, JavaScript can also run on the server.

What can you do with JavaScript?

With JavaScript you can build interactive websites, calculators, games, forms, menus, and other web applications.

For example

const name = "Nibras Code";
console.log("Hello, " + name);

This code uses the name "Nibras Code" and writes a greeting to the console.

Is JavaScript difficult to learn?

You can learn the basics of JavaScript, but using the language well takes steady practice.

At the start, these topics are useful:

  1. Variables
  2. Data types
  3. Conditions
  4. Loops
  5. Functions
  6. Arrays and objects
  7. Working with the DOM
  8. Events
  9. Asynchronous JavaScript
  10. APIs

Advantages and disadvantages of JavaScript

Advantages

  • It can run directly in the browser
  • It is widely used in web programming
  • It has a large ecosystem of libraries and frameworks
  • It can be used for frontend and backend work
  • It has a large programmer community
  • It can be used on many platforms

Disadvantages

  • Some features of the language can confuse a beginner
  • Asynchronous programming can look difficult at first
  • A large project needs a clear code structure
  • The number of libraries and frameworks can make the first choice hard

JavaScript syntax and basic concepts

In JavaScript, variables are created with keywords such as "let" and "const".

const name = "Ali";
let age = 20;

Condition

if (age >= 18) {
    console.log("Adult");
}

Loop

for (let i = 1; i <= 5; i++) {
    console.log(i);
}

Function

function greet(name) {
    return "Hello, " + name;
}

console.log(greet("Ali"));

JavaScript code examples

A simple message

console.log("Hello, world!");

Addition

let a = 10;
let b = 20;

console.log(a + b);

Checking a number

let number = 10;

if (number > 0) {
    console.log("Positive number");
}

An array

const fruits = ["Apple", "Banana", "Orange"];

for (const fruit of fruits) {
    console.log(fruit);
}

Working examples

The short lines above show one idea. These examples carry a small job from the start to the end. Open the browser console, paste the code, and press Enter. Then change a name or a number and run it again.

Write names from a list

Several names stand in one list. for takes each name in turn and writes it. If you add a name, the loop writes that too. You do not write a separate line for every name.

const names = ["Aysel", "Murad", "Nigar"];
for (const name of names) {
  console.log("Salam, " + name);
}

Three lines appear: Salam, Aysel, then Murad, then Nigar.

Remember one student

An object keeps several facts about one thing. The name and the score stay together. You write the key after a dot, and the value comes back. If the score is 50 or more, Keçdi is written. Set the score to 40 and that line stays silent.

const student = { name: "Aysel", score: 85 };
console.log(student.name);
if (student.score >= 50) {
  console.log("Keçdi");
}

Aysel comes first, then Keçdi. A wrong key gives undefined, not a guess.

Find the average

total starts at zero and each score is added. At the end the total is divided by how many numbers are in the list. The average of 70, 80, and 90 is 80. Add a fourth score and the divisor changes by itself.

const scores = [70, 80, 90];
let total = 0;
for (const score of scores) {
  total = total + score;
}
const average = total / scores.length;
console.log(average);

80 appears. This example does not round the number.

Change a button when it is pressed

This is the job JavaScript is known for. querySelector finds the first button on the page. addEventListener waits for a click. When the click comes, the button's text becomes Basıldı. If the page has no button, querySelector returns nothing and the next line fails. Put a button in the HTML first.

const button = document.querySelector("button");
button.addEventListener("click", function () {
  button.textContent = "Basıldı";
});

Before the click the button keeps its old text. After the click only the text changes. The page does not reload.

Frequently asked questions about JavaScript

What is JavaScript?

JavaScript is a programming language mainly used to build interactive and dynamic websites.

Is JavaScript free?

Yes. Using JavaScript does not require a separate license fee.

Can you build a website with JavaScript?

Yes. Together with HTML and CSS, JavaScript is widely used to build modern websites.

Can you build a mobile app with JavaScript?

Yes. Various frameworks and technologies make it possible to build mobile applications with JavaScript.

Can JavaScript run on the server?

Yes. For example, Node.js can run JavaScript on the server.

Are JavaScript and Java the same?

No. The names are similar, but Java and JavaScript are different programming languages.

Tool for this topic