#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Types and Statements

A JavaScript program is built from statements, and statements work with values of different types. A statement is an instruction that does something: declare a variable, make a decision, repeat a block. An expression is a piece of code that produces a value. Seeing the difference between the two, and knowing the basic value types, makes error messages and code structure much easier to understand.

Values and their types, briefly

Every value has a type. The everyday ones are numbers (7, 2.5), strings ('hello'), booleans (true, false), undefined, null, and objects, which include arrays and functions. JavaScript decides types while the program runs, so the same variable can hold a number now and a string later. The following lessons explore each type in detail; for now it is enough to know that a value's type decides what operations make sense on it.

Expressions produce values

Anything that results in a value is an expression: 3 + 4, userName, price * 1.18, isReady ? 'Go' : 'Wait' or a function call like Math.max(2, 9). Expressions can be nested inside one another and placed anywhere a value is expected, such as on the right side of = or inside a template literal.

The main kinds of statements

KindExamplesPurpose
Declarationlet, const, function, classCreate variables, functions and classes.
Expression statementcount++;, save();An expression used on its own line for its effect.
Conditionalif...else, switchChoose which code runs.
Loopfor, for...of, while, do...whileRepeat code.
Jumpreturn, break, continue, throwLeave a function, loop or block early.
Error handlingtry...catch...finallyRecover from errors.
Block{ ... }Group statements and create a scope for let and const.

Statements in a small program

const marks = [72, 45, 88, 30];      // declaration with an expression
let passed = 0;                      // declaration

for (const mark of marks) {          // loop statement
  if (mark < 0) {                    // conditional
    throw new Error('Invalid mark'); // jump: throw
  }
  if (mark >= 40) {
    passed++;                        // expression statement
  } else {
    continue;                        // jump: skip to the next mark
  }
}

const label = passed === marks.length ? 'All passed' : `${passed} passed`;
console.log(label);                  // "3 passed"

switch (typeof label) {
  case 'string':
    console.log('label is text');
    break;                           // without break, the next case runs too
  default:
    console.log('unexpected type');
}

Why the difference matters

Some places accept only expressions. Inside a template literal ${...}, as a function argument, or inside JSX braces, you cannot write an if statement; you need an expression form such as the ternary operator. Knowing that if is a statement and ? : is an expression tells you which to use.

Statements are usually ended with a semicolon. Block statements like if, for and function declarations end with a closing brace and do not need one.

A mistake with return and line breaks

JavaScript inserts semicolons automatically in some places, and one of them causes a famous bug. If you write return on its own line and the value on the next, JavaScript reads it as return; and the function returns undefined:

return followed by a newline and then { ok: true } returns nothing. Keep the value on the same line as return, or start the opening brace or bracket on that line. A related slip is leaving break out of a switch case, so execution falls through into the next case.

Main points

Expressions produce values; statements perform actions and give a program its structure. Use declarations, conditionals, loops and jump statements to control flow, and choose expression forms where only values are allowed. Keep returned values on the same line as return, and end each switch case with break.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course