Javascript Statements And How To Use Them

Tanggal : 15 Feb 2023, Category : JavaScript, Create By : admin

JavaScript statements are individual instructions that tell the browser or JavaScript engine what to do. Here are some common types of JavaScript statements:

  1. Expression statements: An expression statement is simply an expression followed by a semicolon. An expression is any valid unit of code that can be evaluated to a value. For example:
    x = 5;
    console.log(x);

    In this code, x = 5 is an expression that sets the value of x to 5, and console.log(x) is an expression that logs the value of x to the console.

  2. Conditional statements: A conditional statement allows your code to make decisions based on certain conditions. The most common type of conditional statement is the if statement. For example:
    let x = 5;
    if (x > 0) {
    console.log('x is positive');
    } else {
    console.log('x is negative');
    }

    In this code, the if statement checks whether x is greater than 0, and if it is, it logs 'x is positive' to the console. If x is not greater than 0, it logs 'x is negative'.

  3. Loop statements: A loop statement allows your code to repeat a set of instructions multiple times. The most common type of loop statement is the for loop. For example:
    for (let i = 0; i < 5; i++) {
    console.log(i);
    }

    In this code, the for loop runs five times, with the variable i taking on the values 0, 1, 2, 3, and 4 in each iteration. The loop body logs the value of i to the console in each iteration.

  4. Function statements: A function statement allows you to define a reusable block of code. For example:
    function sayHello(name) {
    console.log('Hello, ' + name + '!');
    }

    sayHello('John'); // logs 'Hello, John!'

    In this code, the sayHello function takes a name parameter and logs a greeting to the console.

To use JavaScript statements, simply write the appropriate code in your script file or console. Statements are executed in the order in which they appear in your code, so make sure to write them in the correct order to achieve the desired result.


Keyword:
  • Javascript Statements And How To Use Them

Artikel Lain

Pengertian Dan Fungsi Css Dalam Pembuatan Website

Date : 15 Aug 2018, Category : Pemprograman Website, Create By : admin

CSS adalah singkatan dari Cascading Style Sheet atau di terjemahkan di google translate menjadi Lembar bergaya susun. Yaitu sebuah perintah atau intruksi yang dapat digunakan untuk menampilkan text dengan design yang menarik untuk sebuah .....

Cara Agar Tanda Petik Bisa Tersimpan Di Database

Date : 09 Jan 2019, Category : Tutorial, Create By : admin

Selamat datang di gudang ilmu aplikasi, kali ini kami akan membahas cara agar tanda petik bisa tersimpan di database, pasti bagi anda yang pernah membuat aplikasi pernah mengalami error ketika .....

Html Headings And How To Use Them

Date : 15 Feb 2023, Category : HTML, Create By : admin

HTML headings are used to define the structure and hierarchy of content on a web page. There are six levels of headings in HTML, from H1 to H6, where H1 .....