Día 4 de JavaScript completo en 17 días | Serie Visual

Descubre qué son truthy y falsy en JavaScript, con ejemplos prácticos y diferencias entre || y &&, y su uso en Node.js. Guía de buenas prácticas y servicios de desarrollo a medida de Q2BSTUDIO.

20 sept 2025 • 3 min read • Q2BSTUDIO Team

Inteligencia-Artificial-

Día 4 de JavaScript completo en 17 días | Serie Visual: Valores truthy y falsy en JavaScript explicados de forma simple

Al programar en JavaScript es común encontrarse con expresiones que aprovechan cómo el lenguaje decide qué valor usar cuando evalúa condiciones. Entender values truthy y falsy ayuda a escribir código más limpio y evitar errores sutiles.

Qué son falsy y truthy: en JavaScript algunos valores se consideran falsos al evaluarlos en un contexto booleano. Los valores falsy comunes son 0, cadena vacía, null, undefined, NaN y false. Todos los demás valores se consideran truthy, por ejemplo cualquier cadena con contenido como Azaan, objetos vacíos {}, arreglos [], e incluso la cadena 0 se considera truthy.

Ejemplo conceptual: const firstName = cadena vacía; const nickName = Azzu; console.log(firstName || nickName); resultado Azzu. Si firstName es falsy, el operador || selecciona el siguiente valor truthy disponible.

Algunos ejemplos prácticos: let fname = cadena vacía; let mname = undefined; let lname = null; console.log(fname || mname || lname); resultado null. Otro ejemplo: let a = 12; let b; console.log(a + b); resultado NaN. Para evitar NaN al sumar con una variable indefinida se puede usar c + (d || 0) por ejemplo let c = 12; let d; console.log(c + (d || 0)); resultado 12.

Diferencias entre operadores: || devuelve el primer valor truthy que encuentre o el último valor si todos son falsy. && devuelve el primer valor falsy que encuentre o el último valor si todos son truthy. Ejemplos: let f1 = Azaan; let f2 = cadena vacía; let ans = f1 && f2; console.log(Name- ans); resultado Name- . Otro: let f3 = Azaan; let f4 = null; let ans2 = f3 && f4; console.log(Name- ans2); resultado Name-null.

Uso real en backend con Node.js: const PORT = process.env.PORT || 5000; significa usar el puerto definido en el entorno si existe, en caso contrario usar 5000. Este patrón es habitual al desplegar aplicaciones y es un ejemplo claro de software a medida y buenas prácticas en configuración de servicios.

En Q2BSTUDIO somos una empresa de desarrollo de software y aplicaciones a medida que además integra servicios avanzados como inteligencia artificial, ciberseguridad, servicios cloud y soluciones de inteligencia de negocio. Si necesitas una solución personalizada puedes contactar con nuestro equipo de Desarrollo de aplicaciones y software a medida y también ofrecemos despliegues y arquitectura en la nube con servicios cloud aws y azure. Entre nuestros servicios destacamos ia para empresas, agentes IA, power bi, servicios inteligencia de negocio y pentesting para garantizar seguridad.

Mini quiz y respuestas rápidas: console.log(3 || 2 || 1); resultado 3. console.log(cadena vacía || 0 || 2); resultado 2. console.log(null || 2 || 3); resultado 2. console.log(cadena vacía || 0 || undefined); resultado undefined. console.log(cadena con texto undefined || cadena con texto null || 2); resultado cadena con texto undefined. console.log(3 && 2 && 1); resultado 1. console.log(cadena vacía && 0 && 2); resultado cadena vacía. console.log(undefined && cadena con texto null && 2); resultado undefined.

Conclusión breve: dominar cómo JavaScript interpreta truthy y falsy facilita manejar operadores lógicos, establecer valores por defecto y prevenir errores como NaN o valores inesperados en producción. Si buscas desarrollar una aplicación a medida, integrar IA para empresas o proteger tus sistemas con ciberseguridad profesional, en Q2BSTUDIO podemos ayudarte a implementar estas buenas prácticas en tus proyectos.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Artificial intelligence

AI agents, chatbots, and intelligent assistants that automate tasks and serve your customers 24/7 to improve the efficiency of your business.

More info

Software Development

Web, mobile, and desktop applications, intranets, e-commerce, SaaS, and management platforms designed for your company's specific needs.

More info

Cloud services

Migration, infrastructure, managed hosting, high availability, and security on Microsoft Azure and Amazon Web Services to help your business scale without limits.

More info

Cybersecurity and pentesting

Security audits, penetration testing and protection of applications, data and infrastructure on-premise and cloud, with ethical hacking and regulatory compliance.

More info

Business Intelligence

Dashboards and data analysis with Power BI: we integrate your sources, design dashboards and KPIs and turn your data into decisions.

More info

Process automation

We automate repetitive tasks and connect your applications with n8n, Power Automate, Make, and RPA, eliminating manual work and increasing productivity.

More info

Training for Companies

We train your teams in technology with criteria: web development, databases, Git, best practices and security, automation with n8n, artificial intelligence for companies and creation of AI solutions with Azure AI Foundry.

More info

Code Auditing

We audit the code that you, your team or an AI create: we tell you what is good and what to improve, we secure it and make it ready for production, web or app.

More info

AI Image Generation

We create for you the images that your business needs with artificial intelligence: product, networks, advertising, illustration and avatars. You tell us what you want and we deliver it ready to use.

More info

AI Video Generation

We create videos with artificial intelligence for you: promotional, networking, virtual presenters, dubbing and animations. You tell us the idea and we will deliver it assembled and ready to publish.

More info

AI Conversational Avatars

We create conversational avatars with AI – digital humans with a face and voice – that serve your customers and teams with the knowledge of your company, on your website, interactive monitors, WhatsApp or Teams.

More info

Online Marketing and AI

Google Ads, Meta Ads, LinkedIn Ads and AI Engine Positioning (GEO/AEO): we attract customers and make your brand appear where they search for you, also on ChatGPT, Gemini and Perplexity.

More info

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.

Live Chat